php \n 对比 PHP_EOL 对比 <br>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21373478/
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
\n vs. PHP_EOL vs. <br>?
提问by John Yin
In most cases, as for one interactive website, when we output multiple lines of contents to web client browser, in my opinion, <BR />is much more preferable than other two: \nor PHP_EOL.
在大多数情况下,对于一个交互式网站,当我们将多行内容输出到Web客户端浏览器时,我认为<BR />比其他两个要好得多:\n或PHP_EOL。
Else, we need to use "<pre></pre>" to wrap the output content or use nl2br()to insert <BR />before \nso as the multiple line mark can take effect in HTML. Like following example.
否则,我们需要使用“ <pre></pre>”来包装输出内容或使用之前nl2br()插入<BR />,\n以便多行标记在HTML中生效。像下面的例子。
$fruits = array('a'=>'apple', 'b'=>'banana', 'c'=>'cranberry');
// Multiple lines by \n
foreach( $fruits as $key => $value ){
echo "$key => $value \n" ;
}
// Multiple lines by PHP_EOL
reset( $fruits );
while ( list($key, $value) = each( $fruits ) ){
echo ("$key => $value" . PHP_EOL);
}
// Multiple lines by <BR />
reset( $fruits );
while ( list($key, $value) = each( $fruits ) ){
echo ("$key => $value <BR />");
}
Some people believe PHP_EOLis useful when writing data to a file, example a log file. It will create line breaks no matter whatever your platform.
有些人认为PHP_EOL在将数据写入文件(例如日志文件)时很有用。无论您使用什么平台,它都会创建换行符。
Then, my question is when we use \n? What's the difference between \nand PHP_EOL, and <BR />? Could any body have a big list of each of their pros and cons?
然后,我的问题是我们\n什么时候使用? \n和PHP_EOL和 和 和有<BR />什么区别?任何机构都可以列出每个优点和缺点的大清单吗?
回答by Nick Coons
DOS, Unix, and Mac (pre-OS X and OS X) all use different characters or character combinations to represent "go to the next line."
DOS、Unix 和 Mac(OS X 和 OS X 之前)都使用不同的字符或字符组合来表示“转到下一行”。
DOS - Uses a CR+LF (that's ASCII 13 followed by an ASCII 10, or
\r\n) to represent a new line.Unix - Uses an LF (that's ASCII 10, or
\n) to represent a new line.Mac (pre-OS X) - Uses a CR (that's ASCII 13, or
\r) to represent a new line.Mac (OS X) - Like Unix, uses an LF to represent a new line.
DOS - 使用 CR+LF(即 ASCII 13 后跟 ASCII 10 或
\r\n)来表示新行。Unix - 使用 LF(即 ASCII 10 或
\n)来表示新行。Mac(OS X 之前的版本)- 使用 CR(即 ASCII 13 或
\r)来表示新行。Mac (OS X) - 与 Unix 一样,使用 LF 表示新行。
Therefore, when to use each one depends on what you're going for. If you're writing for a specific platform without the intention of portability, use the character or character combination to break lines that matter to that platform. The purpose of PHP_EOLis to automatically choose the correct character for the platform, so that your new lines are platform-independent.
因此,何时使用每个取决于您的目的。如果您正在为特定平台编写而不打算移植,请使用字符或字符组合来中断对该平台重要的行。的目的PHP_EOL是自动为平台选择正确的字符,使您的新行与平台无关。
All of these appear as a single space within a browser as browsers collapse whitespace into a display space for display purposes (unless you're using <pre>as you mentioned, or CSS that changes the behavior of whitespace). This is where <br>comes in, as you've mentioned, which will convert these \nnew line characters into <br>so that they provide line breaks in HTML display.
所有这些都在浏览器中显示为单个空间,因为浏览器将空白折叠成显示空间以进行显示(除非您<pre>按照您提到的方式使用,或者更改了空白行为的 CSS)。<br>正如您所提到的,这就是进来的地方,它将这些\n新行字符转换为,<br>以便它们在 HTML 显示中提供换行符。
回答by m59
<br>
<br>
<br>is only to be used when dividing up text. You may see this used for spacing out elements within an html document, but that is not its purpose, as layout spacing should be achieved with CSS such as margin. Here's a decent use case:
<br>仅在分割文本时使用。您可能会看到这用于分隔 html 文档中的元素,但这不是它的目的,因为布局间距应该使用 CSS 来实现,例如margin. 这是一个不错的用例:
<p>This <br> is <br> some <br> text.</p>
I typically break up my text with linebreaks for readability, so I wouldn't want the linebreaks rendered in the document. That being, the <br>tag is necessary.
我通常用换行符来分解我的文本以提高可读性,所以我不希望在文档中呈现换行符。也就是说,<br>标签是必要的。
CSS whitespace, <pre>
CSS 空格,<pre>
The html element <pre>is an element that, by default, uses CSS rule white-space: pre;so that the document's whitespace is rendered within that element. Normally, only a single space would be rendered regardless of the amount of whitespace in the document. A very good use of this style is for code blocks. If I write a code block, I will naturally space it as I intend and don't particularly care to wrap a <p>tag around each line (that's not semantic anyway) and I don't want to litter my code with <br>either. Even worse, there's the code indentation! whitespace: premakes a lot of sense here.
html 元素<pre>是一个默认情况下使用 CSS 规则的元素,white-space: pre;以便在该元素内呈现文档的空白。通常,无论文档中有多少空白,都只会呈现一个空白。这种风格的一个很好的用途是用于代码块。如果我写了一个代码块,我会自然地按照我的意图将它隔开,并且并不特别关心<p>在每一行周围包裹一个标签(这不是语义上的),我也不想在我的代码中乱扔垃圾<br>。更糟糕的是,还有代码缩进!whitespace: pre在这里很有意义。
<pre>
function foo() {
//etc
}
</pre>
\r, \n, PHP_EOL
\r, \n, PHP_EOL
Depending on the platform your php code is running from, you'll need to render a linebreak with \r, \n\, or both \r\n. PHP_EOLwill automatically choose for you, so it is the most dynamic approach.
根据你的PHP代码从运行的平台上,你需要渲染断行用\r,\n\或两者\r\n。PHP_EOL会自动为你选择,所以这是最有活力的方法。
回答by kba
It makes no sense to compare a HTML linebreak <br>with actual text linebreaks. <br>is only a linepage in the browser's rendering of a website whereas \nand PHP_EOLare actual, source code linebreaks.
将 HTML 换行符<br>与实际文本换行符进行比较是没有意义的。<br>仅在一个网站的,而浏览器的渲染linepage\n和PHP_EOL是实际的,源代码换行。
As you mention yourself, PHP_EOLwill work regardless of platform and is therefore also preferred over \n. Initially, \nwas only the standard linebreak on UNIX, whereas Macintosh used \rand Windows/DOS used a combination of the two: \r\n. When using PHP_EOL, however, you don't have to worry about platform at all.
正如您自己提到的,PHP_EOL无论平台如何都可以工作,因此也优于\n. 最初,\n它只是 UNIX 上的标准换行符,而 Macintosh 使用\r和 Windows/DOS 使用两者的组合:\r\n. PHP_EOL但是,在使用时,您根本不必担心平台。
Nowadays, most systems and editors are tolerant enough to accept whatever linebreak you decide to use, but using the correctone is obviously preferred and most-easily achieved with PHP_EOL, so there's really no reason not to use it.
如今,大多数系统和编辑器都足以接受您决定使用的任何换行符,但使用正确的换行符显然是首选且最容易实现的PHP_EOL,因此真的没有理由不使用它。
回答by Flavio
I have to run scripts on cron and save results to a log file. But sometimes I run the script on my browser. So I do the following test in order to use the correct end of line char:
我必须在 cron 上运行脚本并将结果保存到日志文件中。但有时我会在浏览器上运行脚本。所以我做了以下测试以使用正确的行尾字符:
<?php
$eol = (isset($_SERVER['SHELL'])) ? PHP_EOL : "<br />";
echo "DISCOVER". $eol;
?>
回答by Peter
You can create a function with Flavio's code, that always sets the correct newline, no matter if you run it on the shell or in the browser:
您可以使用 Flavio 的代码创建一个函数,该函数始终设置正确的换行符,无论您是在 shell 还是在浏览器中运行它:
<?php
function nl($string) {
if(isset($_SERVER['SHELL'])) return preg_replace('/\<br(\s*)?\/?\>/i', PHP_EOL, $string);
return nl2br($string);
}
print nl("One\nTwo<br>Three\r\nFour<br />Five
Six" . PHP_EOL);
?>
回答by Sayed Mohd Ali
<br>
<br>
It was created for HTML designing... <br>will not work in many places until it supports content type is <HTML>
它是为 HTML 设计而创建的......<br>在它支持内容类型之前不会在很多地方工作<HTML>
an example1 in mail functions where the content type is text... <br>will not work but /nwill work.
内容类型为文本的邮件函数中的示例 1...<br>将不起作用,但/n会起作用。
more examples where you will see different results.
更多示例,您将看到不同的结果。
example 2
例子2
<html>
<body>
<script>
document.write("Hello \n World!");
</script>
</body>
</html>
As document.write writes to the DOM you need to use
for a newline.
Relevant MDN docs
当 document.write 写入需要
用于换行的 DOM 时。
相关的 MDN 文档
example3
例子3
txt = "Hello \n World!" ;
alert(txt);
because alertfunction is asking a message to be shown to the user
it isn't the same as a popup window with HTML in it
因为alert函数要求向用户显示一条消息,所以它与包含 HTML 的弹出窗口不同
/n
/n
is basically common in most of the programming languages for a line break and it will work within most of the platforms. \nand PHP_EOLare actual, source code linebreaks.
在大多数编程语言中,换行符基本上很常见,并且可以在大多数平台上使用。\n并且PHP_EOL是实际的源代码换行符。
The constant PHP_EOL should generally be used for platform-specific output.
常量 PHP_EOL 通常应用于特定于平台的输出。
Mostly for file output really.
Actually, the file functions already transform \n ←→ \r\non Windows systems unless used in fopen(…, "wb")binary mode.
For file input, you should prefer \nhowever. While most network protocols (HTTP) are supposed to use \r\n,that's not guaranteed.
主要用于文件输出。实际上,\n ←→ \r\n除非在fopen(…, "wb")二进制模式下使用,否则文件函数已经在 Windows 系统上进行了转换。\n但是,对于文件输入,您应该更喜欢。虽然大多数网络协议 (HTTP) 都应该使用\r\n,,但并不能保证。
Therefore it's best to break up on \nand remove any optional \rmanually:
因此,最好手动拆分\n并删除任何可选项\r:
example
例子
$lines = array_map("rtrim", explode("\n", $content));
A more robust and terser alternative is using preg_split() and a regexp:
一个更健壮和更简洁的替代方法是使用 preg_split() 和一个正则表达式:
$lines = preg_split("/\R/", $content);
The \Rplaceholder detects any combination of \r + \n. So would be safest, and even work for Classic MacOS ≤ 9text files (rarely seen in practice).
该\R占位符检测的任何组合\r + \n。所以最安全,甚至适用于经典MacOS ≤ 9文本文件(在实践中很少见)。
Beware that old Macs use \r, this would be a better solution: preg_replace('~\r\n?~', "\n", $str);
请注意旧 Mac 使用 \r,这将是一个更好的解决方案: preg_replace('~\r\n?~', "\n", $str);
PHP_EOL
PHP_EOL
should be used when writing output such as log files.
应该在写入日志文件等输出时使用。
It will produce the line break specific to your platform.
它将产生特定于您的平台的换行符。
it is a constant holding the line break character(s) used by the server platform. In the case of Windows, it's \r\n. On *nix, it's \n. You apparently have a Windows server.
它是一个常量,包含服务器平台使用的换行符。在 Windows 的情况下,它是 \r\n。在 *nix 上,它是 \n。您显然有一台 Windows 服务器。

