PHP向txt檔案裡面寫入字串,換行問題

來源:互聯網
上載者:User
$DOCUMENTROOT = $_SERVER['DOCUMENT_ROOT'];    $file = fopen("$DOCUMENTROOT/php_02/123.txt","wb");    fwrite($file,"line 1 "."\n"." line 2 n line 3");    fclose($file);

這樣沒用
但是
$DOCUMENTROOT = $_SERVER['DOCUMENT_ROOT'];    $file = fopen("$DOCUMENTROOT/php_02/123.txt","wb");    fwrite($file,"line 1 "."\r\n"." line 2 n line 3");    fclose($file);

這樣就可以了,問問為什麼


回複討論(解決方案)

是不同的編輯器解析效果不同

在命令列裡面分別開啟以下兩個程式,然後用程式開啟產生的檔案,就可以看出程式是怎麼解析\n的了
notepad
wordpad

Windows下很多編輯器把\r\n當換行,忽略\n

你把Word顯示控制字元功能開啟,\n顯示為向下的箭頭,\r\n顯示為拐彎的箭頭

window \r\n
linux \n

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.