PHP Echo 换行符
\n 和 \r 之间有什么区别(我知道它与操作系统有关),以及回显跨平台工作的换行符的最佳方法是什么?
编辑:作为对 Jarod 的回应,我将使用 ths 来回显 .txt 日志文件中的换行符,尽管我确信将来我会使用它来执行诸如将 HTML 布局回显到页面上。
What's the difference between \n and \r (I know it has something to do with OS), and what's the best way to echo a line break that will work cross platform?
EDIT: In response to Jarod, I'll be using ths to echo a line break in a .txt log file, though I'm sure I'll be using it in the future for things such as echoing HTML makup onto a page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
PHP_EOL
常量,该常量会自动设置为运行 PHP 脚本的操作系统的正确换行符。请注意,该常量是从 PHP 5.0.2 开始声明的。
为了向后兼容:
Use the
PHP_EOL
constant, which is automatically set to the correct line break for the operating system that the PHP script is running on.Note that this constant is declared since PHP 5.0.2.
For backwards compatibility:
\n
是 Linux/Unix 换行符。\r
是经典的 Mac OS(非 OS X)换行符。 Mac OS X 使用上面的 unix\n
。\r\n
是 Windows 换行符。我通常只在 Linux 系统上使用
\n
,而且大多数 Windows 应用程序都可以正常处理它。\n
is a Linux/Unix line break.\r
is a classic Mac OS (non-OS X) line break. Mac OS X uses the above unix\n
.\r\n
is a Windows line break.I usually just use
\n
on our Linux systems and most Windows apps deal with it ok anyway.Jarod 的答案包含 \r \n 在各种操作系统上的正确用法。 这里有一些历史:
术语“回车”和“换行”可以追溯到使用电传打字机代替带有显示器和键盘的终端时。 对于电传打字机或打字机,“回车”意味着移动光标并返回到文本的第一列,而“换行”意味着旋转滚轮以进入下一行。 当时这种区别是有道理的。 如今,表示一行文本结尾的组合 \n、\r、\r\n 是完全任意的。
Jarod's answer contains the correct usage of \r \n on various OS's. Here's some history:
The terminology "carriage return" and "line feed" dates back to when teletypes were used instead of terminals with monitor and keyboard. With respect to teletypes or typewriters, "carriage return" meant moving the cursor and returning to the first column of text, while "line feed" meant rotating the roller to get onto the following line. At that time the distinction made sense. Today the combinations \n, \r, \r\n to represent the end of a line of text are completely arbitrary.
PHP4 上的 PHP_EOL 不需要向后兼容。
需要更正 Moore 关于 PHP_EOL 持续可用性的声明:“...自 PHP 5.0.2 起声明。”。
不,它从 PHP 4.3.10 就已经存在了。 任何仍在经营低于此水平的公司的人无论如何都不应该从事商业活动。 截至今天,没有人应该使用低于 PHP 5 的任何版本!
来自 PHP 手册:“PHP_EOL 此平台的正确‘行尾’符号。自可用以来PHP 4.3.10 和 PHP 5.0.2”。
No backwards compatibility necessary for PHP_EOL on PHP4.
Need to correct Moore's statement on constant PHP_EOL availability: "... is declared since PHP 5.0.2.".
No, it has been around since PHP 4.3.10. Anyone who is still running anything lesser than that should not be in biz anyhow. As of today no one should be using anything lesser than PHP 5!
From the PHP manual: "PHP_EOL The correct 'End Of Line' symbol for this platform. Available since PHP 4.3.10 and PHP 5.0.2".