从fread txt文件更改空间

时间:2017-01-13 09:01:24

标签: php

美好的一天。我试图阅读txt文件。所以这是txt

中的内容
SS,SS,SS,BSM160801870,1
SS,SS,SS,BSM160801825,1
SS,SS,SS,BSM160801812,1
SS,SS,SS,BSM160701676,1
SS,SS,SS,BSM160501311,1
SS,SS,SS,BSM160501351,1
SS,SS,SS,BSM160701721,1
SS,SS,SS,BSM160501269,1

所以我用这个

读取文件
$myfile = fopen($file, "r") or die("Unable to open file!");
$textfile = fread($myfile,filesize($file));
echo $textfile;

但结果是这样的

SS,SS,SS,BSM160801870,1 SS,SS,SS,BSM160801825,1 SS,SS,SS,BSM160801812,1 SS,SS,SS,BSM160701676,1 SS,SS,SS,BSM160501311,1 SS,SS,SS,BSM160501351,1 SS,SS,SS,BSM160701721,1 SS,SS,SS,BSM160501269,1

所以我想在结果的每个;中给space,所以我尝试使用这个

$myfile = fopen($file, "r") or die("Unable to open file!");
$textfile = fread($myfile,filesize($file));
$textfiles = str_replace(" ",";",$textfile);
echo $textfiles;

但没有帮助。

我想要的结果是这样的

SS,SS,SS,BSM160801870,1;SS,SS,SS,BSM160801825,1;SS,SS,SS,BSM160801812,1;SS,SS,SS,BSM160701676,1;SS,SS,SS,BSM160501311,1;SS,SS,SS,BSM160501351,1;SS,SS,SS,BSM160701721,1;SS,SS,SS,BSM160501269,1

我怎样才能实现这一目标?提前谢谢。

如果我这样做,

工作正常

$string = "SS,SS,SS,BSM160801870,1 SS,SS,SS,BSM160801825,1 SS,SS,SS,BSM160801812,1 SS,SS,SS,BSM160701676";  
        $textfiles = str_replace(" ",";",$string);
        echo $textfiles;

1 个答案:

答案 0 :(得分:1)

在回显文件内容时,所有新行都将转换为简单空格。

您需要将\r\n替换为; preg_replace('/\r\n/', ';', file_get_contents($myfile))