用PHP编写TXT文件,想要添加实际的换行符

时间:2022-01-05 07:23:19

I am writing a TXT file using PHP. I want to insert actual line breaks into the TXT file wherever necessary. I have tried all combinations of \n \r \r\n \n\r ... but these are not causing any linebreaks to appear - in most cases, I am seeing the text "\n" appear in the TXT file, with no linebreak.

我正在使用PHP编写TXT文件。我想在必要时在TXT文件中插入实际的换行符。我已经尝试过\ n \ r \ r \ n \ n \ n \ r \ n的所有组合......但是这些并没有导致出现任何换行符 - 在大多数情况下,我看到文本“\ n”出现在TXT文件中,没有换行。

I have also tried chr(13).

我也试过chr(13)。

Any other ideas would be appreciated.

任何其他想法将不胜感激。

5 个解决方案

#1


19  

Sounds to me like you might be using single quotes, i.e. '\n' rather than "\n".

听起来像你可能使用单引号,即'\ n'而不是“\ n”。

If you wanted to continue with a single quotes bias (as you should!), two options:

如果你想继续使用单引号偏差(正如你所希望的那样!),有两个选择:

file_put_contents('/path/to/file.txt', 'Hello friend!
This will appear on a new line.
As will this');

// or

file_put_contents('/path/to/file.txt', 'Hello friend!'."\n".'This will appear on a new line.'."\n".'As will this');

#2


29  

For "\n" to work, you need to use double quotes, not '\n'.

要使“\ n”起作用,您需要使用双引号,而不是'\ n'。

But you should use the constant PHP_EOL instead, so that it adapts automatically to the OS ("\n", "\r" or "\r\n").

但是你应该使用常量PHP_EOL,以便它自动适应OS(“\ n”,“\ r”或“\ r \ n”)。

file_put_contents('file.txt', 'Bla' . PHP_EOL . 'Bla');

#3


3  

\r\n in a windows server \n in linux Make sure you upload the file as ASCII.

\ r \ n在Windows服务器上\ n在linux中确保将文件上传为ASCII。

#4


1  

You must write \n in a double-quoted string (in single-quoted strings no parsing takes place):

您必须在双引号字符串中写入\ n(在单引号字符串中不进行解析):

"foo\r\nbar"

Further reference:

http://es2.php.net/manual/en/language.types.string.php

#5


1  

you could also use chr(10) which is line break.

你也可以使用chr(10)这是换行符。

#1


19  

Sounds to me like you might be using single quotes, i.e. '\n' rather than "\n".

听起来像你可能使用单引号,即'\ n'而不是“\ n”。

If you wanted to continue with a single quotes bias (as you should!), two options:

如果你想继续使用单引号偏差(正如你所希望的那样!),有两个选择:

file_put_contents('/path/to/file.txt', 'Hello friend!
This will appear on a new line.
As will this');

// or

file_put_contents('/path/to/file.txt', 'Hello friend!'."\n".'This will appear on a new line.'."\n".'As will this');

#2


29  

For "\n" to work, you need to use double quotes, not '\n'.

要使“\ n”起作用,您需要使用双引号,而不是'\ n'。

But you should use the constant PHP_EOL instead, so that it adapts automatically to the OS ("\n", "\r" or "\r\n").

但是你应该使用常量PHP_EOL,以便它自动适应OS(“\ n”,“\ r”或“\ r \ n”)。

file_put_contents('file.txt', 'Bla' . PHP_EOL . 'Bla');

#3


3  

\r\n in a windows server \n in linux Make sure you upload the file as ASCII.

\ r \ n在Windows服务器上\ n在linux中确保将文件上传为ASCII。

#4


1  

You must write \n in a double-quoted string (in single-quoted strings no parsing takes place):

您必须在双引号字符串中写入\ n(在单引号字符串中不进行解析):

"foo\r\nbar"

Further reference:

http://es2.php.net/manual/en/language.types.string.php

#5


1  

you could also use chr(10) which is line break.

你也可以使用chr(10)这是换行符。