2013-07-27 73 views
1

我試着用下面的代碼更新一個文本文件:新行不創建試圖更新在文本文件中的文本時

$filename = "flat-file-data.txt"; // File which holds all data 
$rowToUpdate = $_REQUEST['number']; // This is line need to be updated 
$newString = $_REQUEST['line'].'\r\n'; // This is what you want to replace it with 

$arrFp = file($filename); // Open the data file as an array 
// Replace the current element in array which needs to be updated with new string 
$arrFp[$rowToUpdate-1] = $newString; 
$numLines = count($arrFp); // Count the elements in the array 

$fp = fopen($filename, "w"); // Open the file for writing 
for($i=0; $i < $numLines; $i++) 
{ 
    fwrite($fp, $arrFp[$i]); 
} 
fclose($fp); // Close the file 

,你可以從上面的代碼看,它會更新從$ newString和它的數據線1條應該能夠創建在每行後一個新的符合這個「\ r \ n」。

不幸的是,它不創建一個新的行,它只是伴隨着同一行或在例如像當前行變爲「這是FIRSTLINE \ r \ n這是第二行」。是否有任何準確的方法,我可以使它在一個新的行如

this is the firstline 
this is the secondline 

等等等等?

回答

1

你必須把\r\n在雙引號:

$newString = $_REQUEST['line']."\r\n"; 
+0

謝謝!像魅力一樣工作! :d –

相關問題