2012-09-24 54 views
-3

如何在每次執行將數據寫入文本文件後打破新行?php繼續用新的換行符寫入文件

該文件是在沒有新的換行顯示記錄:

countryId:20,產品編號:332,狀態:1,OPID:188,XID:pdgje9876,countryId:20,產品編號:334,狀態:0,OPID:188,XID:pfgje3455,

,這是我所需要的輸出:

countryId:20,產品編號:332,狀態:1,OPID:188,XID:pdgje9876 ,

countryId:20,產品編號:334,狀態:0,OPID:188,XID:pfgje3455,

$txt = "log.txt"; 
$fh = fopen($txt, 'a') or die("can't open file"); 

foreach($obj as $key => $value) { 
    print "$key => $value<br>"; 

    $stringData = $key.":".$value.", "; 
    fwrite($fh, $stringData); 

} 
fclose($fh); 

回答

2

在每行末尾添加"\n"

$txt = "log.txt"; 
$fh = fopen($txt, 'a') or die("can't open file"); 

foreach ($objects as $obj) { 
    foreach($obj as $key => $value) { 
    print "$key => $value<br>"; 

    $stringData = $key.":".$value.", "; 
    fwrite($fh, $stringData); 
    } 
    fwrite($fh, "\n"); 
} 
fclose($fh); 
0

您可能希望在行尾添加「\ n」。

$txt = "log.txt"; 
$fh = fopen($txt, 'a') or die("can't open file"); 

foreach($obj as $key => $value) { 
    print "$key => $value<br>"; 
    if ($key == "xld") { 
     $stringData = $key.":".$value.",\n"; // Will add new line after xld key 
    } else { 
     $stringData = $key.":".$value.", "; 
    } 
    fwrite($fh, $stringData); 

} 
fclose($fh); 
相關問題