文件使用新行获取并将内容放入文本文件

时间:2013-05-21 14:43:20

标签: php

我有两个文件

File1.php和File2.php

$hello = file_get_contents("file1.php");
file_put_contents("hello.txt",$hello);

file1.php包含一些示例文本,如下所示

hello \r\n hi \r\n how are you.

我需要的是当我把内容放到hello.txt时,我希望这些单词出现在下面的新行中

hello 
hi
how are you.

我尝试使用\ r \ n和\ n但没有任何作用

注意: file1.php是另一个页面来源的file_get_contents。我只能将<br>标记替换为\r\n,这只是一个示例。我还有其他一些复杂的部分要做。

3 个答案:

答案 0 :(得分:3)

执行str_replace

$hello = str_replace('\r\n', "\r\n", $hello)

请注意,单引号和双引号是不同的......请在此处阅读区别:http://php.net/manual/en/language.types.string.php

答案 1 :(得分:1)

我认为你应该在每一行的末尾加上一些东西,

然后你可以替换它:

   str_replace

功能,我认为这会起作用

str_replace('[EndOfLine]', "\r\n", $string);

答案 2 :(得分:-1)

让您的File1.php包含此文本

hello 
hi
how are you.

并且您的代码可以完美运行!