字符串添加新行,我不会';我不明白为什么


String adding new lines and I don't understand why

我用这段代码打开文件夹中的每个文件,将值减少5,然后覆盖现有内容。

<?php
    foreach (glob("users/*.php") as $filename) {    
        $array = file($filename);
        $wallet = $array[0];
        $time = $array[1];
        $status = $array[2];
        $steamid = $array[3];
    $process = fopen($filename, "w") or die("Unable to open file!");
       $time = $time - 5;
       $newdata = $wallet . "'n" . $time . "'n" . $status . "'n" . $steamid;
       fwrite($process, $newdata);
    fclose($process);
}
?>

在我执行脚本之前,打开的文件如下所示:

680
310
0
74892748232

脚本执行后,文件如下所示:

680
305
0
74892748232

如果再次执行脚本,它会添加更多的行,并且只会更多地打断文件。

文件的字符串在这里创建:

$newdata = $wallet . "'n" . $time . "'n" . $status . "'n" . $steamid;

为什么它在$wallet和$status之后添加一个空行,而在$time之后没有添加?我如何避免这种情况,而是写下:

$wallet
$time
$status
$steamid

提前非常感谢。:)

尝试使用此解决方案

您也可以使用file_put_contents():

file_put_contents($filename, implode("'n", $array) . "'n", FILE_APPEND);

来自这个SO问题https://stackoverflow.com/a/3066811/1301180