Writing TXT File with PHP, Want to Add an Actual Line Break

五迷三道 提交于 2019-11-29 03:23:37

Sounds to me like you might be using single quotes, i.e. '\n' rather than "\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');

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

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

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

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

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

"foo\r\nbar"

Further reference:

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

Colin

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!