PHP fwrite 函数换行

PHP fwrite function new line

我有这个代码:

//This line is for the input that i've looped above my code, it's a URL
$textAr[$i] = str_replace("\r\n","", $textAr[$i]);

//This is for implode purposes
$finalArray = array($textAr[$i], $m1, $m2, $m3, $m4, $m5, $m6);

//When i echo this variable, $test, the URL is in the line along with the implode arrays
$test = implode($finalArray, "***");
echo $test."\n";

//This is for writing into my text file
fwrite($sitesTxt, implode($finalArray, "***")."\n");

我遇到了这样的错误,在我输入 3 URLs 之后,第一个和第二个 URL 在我写入文件后有新行,但最后一个 URL 我输入的内容与内爆数组一致。我什至修剪了 $textArr,但我不断收到新行。

预期输出:

https://docs.google.com***false***false***false***false***false***false***
https://whosebug.com***false***false***false***false***false***false***
https://stackexchange.com***false***false***false***false***false***false***

我在 txt 文件中得到的输出:

https://docs.google.com
***false***false***false***false***false***false***
https://whosebug.com
***false***false***false***false***false***false***
https://stackexchange.com***false***false***false***false***false***false***

根据您的系统,您的行可能不会以 \r\n 组合结尾,而可能只是 \r.

我建议将 str_replace 更改为:

$textAr[$i] = str_replace(array("\r","\n"),"", $textAr[$i]);

或者,更改数组:

$finalArray = array(trim($textAr[$i]), $m1, $m2, $m3, $m4, $m5, $m6);

顺便说一下,虽然它会起作用,但你的内爆参数是相反的:

$test = implode("***", $finalArray);

您应该对 'break-line' 字符使用 PHP_EOL 常量。因为换行符在DOS中是\r\n,而在*nix中就是\n。 因此,您将第一行替换为

$textAr[$i] = str_replace(PHP_EOL, '', $textAr[$i]);