[英]put contents of array into string with new line php
I have a file that contains strings that are on new lines like so: 我有一个文件,其中包含新行中的字符串,如下所示:
string1
string2
I want to remove duplicates so i add them to an array, do unique so removes duplicates and now im trying to remove the array and put the contents into a string putting back the line breaks ("\\n"). 我想删除重复项,所以我将它们添加到数组中,做唯一操作,以便删除重复项,现在我试图删除数组并将内容放入字符串中,以换行符(“ \\ n”)开头。
Code is below: 代码如下:
$input = file_get_contents('/srv/test.m3u');
$input = explode("\n", $input);
$result = array_unique($input);
foreach($result as $value){
$newresult .= $value;
}
echo($newresult); //Comes back everything stuck together no line-breaks etc
use implode to add the "\\n" like this: 使用implode添加“ \\ n”,如下所示:
$input = file_get_contents('/srv/test.m3u');
$input = explode("\n", $input);
$result = array_unique($input);
echo implode("\n", $result);
Try the implode function. 尝试爆破功能。
$input = file_get_contents('/srv/test.m3u');
$input = explode("\n", $input);
$result = array_unique($input);
$newresult = implode("\n",$result);
echo($newresult);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.