PHP echo 相当于记事本的 CR LF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11222604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:00:31  来源:igfitidea点击:

PHP echo the equivalent of CR LF of Notepad

phpencoding

提问by troc

I need to export some data using PHP and for each line I'm adding a \r\n. When I open the exported data file that I downloaded, I see that the \r\nis interpreted as [LF] in Notepad.

我需要使用 PHP 导出一些数据,并为每一行添加一个\r\n. 当我打开下载的导出数据文件时,我看到\r\n在记事本中被解释为 [LF]。

But the application in which I open the file doesn't read the [LF] as a new line.

但是我打开文件的应用程序不会将 [LF] 作为新行读取。

Now if I do a [CR][LF] in Notepad, the application can read the [CR][LF] as a new line.

现在,如果我在记事本中执行 [CR][LF],应用程序可以将 [CR][LF] 作为新行读取。

How can I echo the equivalent of [CR][LF] with PHP?

如何使用 PHP 回显等效于 [CR][LF] 的内容?

采纳答案by troc

Problem solved: the string was passed in the POST parameter. Removing the \r.

问题已解决:字符串在 POST 参数中传递。删除\r。

You just have to do a str_replace("\n", "\r\n", $...);.

你只需要做一个str_replace("\n", "\r\n", $...);.

回答by Mark Baker

It's as simple as doing:

这就像做一样简单:

echo "\r\n";

(note the double quotes)

(注意双引号)

回答by Harald Brinkhof

Do echo PHP_EOL;

echo PHP_EOL;

That way it'll always display the correct linefeed/carriage return combination that's valid for the system you're on, since not all OSes use the same newline convention. (More information: PHP global constants.)

这样,它将始终显示对您所在系统有效的正确换行/回车组合,因为并非所有操作系统都使用相同的换行约定。(更多信息:PHP 全局常量。)