2017-07-18 79 views
0

我有这样的代码:PHP的fwrite功能新线

//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个网址,第一和第二URL都有新的生产线后,我在文件中写,但我输入的最后一个URL与内部阵列一致。我甚至修剪了$ textArr,但我不断收到新的内容。

预期输出:

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

输出I在txt文件让我:

https://docs.google.com 
***false***false***false***false***false***false*** 
https://stackoverflow.com 
***false***false***false***false***false***false*** 
https://stackexchange.com***false***false***false***false***false***false*** 
+0

发表您的完整代码,请(包括循环,例如数据) – vietnguyen09

+0

你能告诉我什么是'$ textArr [$ i]'?你可以回应它吗?或者只是给出示例数据 –

+0

我的代码是垃圾。这是为了noobs。无论如何,我已经接受RichGold的解决方案,它为我工作。谢谢! –

回答

1

根据你的系统,你的行可能不与\ 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); 
+0

我已经添加了修剪,就像你说的,它完美的作品。谢谢! –

+0

欢迎您! – RichGoldMD

0

您应该使用PHP_EOL常量作为'break-line'字符。由于DOS中的折线字符是\r\n,但在* nix中,它只是\n。 所以,你更换行头

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