php 是换行 = \n 还是 \r\n?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4824621/
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 14:30:00  来源:igfitidea点击:

Is a new line = \n OR \r\n?

phpstring

提问by Ryan

I've seen many developers use different methods to split a string by new lines, but i'm confused which is the correct: \r\nOR \nonly?

我见过许多开发人员使用不同的方法将字符串拆分为新行,但我很困惑哪个是正确的:\r\nOR \nonly?

回答by icktoofay

\nis used for Unix systems (including Linux, and OSX).

\n用于 Unix 系统(包括 Linux 和 OSX)。

\r\nis mainly used on Windows.

\r\n主要用于Windows。

\ris used on really old Macs.

\r用于非常老的 Mac。

PHP_EOLconstant is used instead of these characters for portability between platforms.

PHP_EOL为了平台之间的可移植性,使用常量代替这些字符。

回答by krowe

The given answer is far from complete. In fact, it is so far from complete that it tends to lead the reader to believe that this answer is OS dependent when it isn't. It also isn't something which is programming language dependent (as some commentators have suggested). I'm going to add more information in order to make this more clear. First, lets give the list of currentnew line variations (as in, what they've been since 1999):

给出的答案远非完整。事实上,它远未完成,以至于它往往使读者相信这个答案是依赖于操作系统的,而实际上它不是。它也不是依赖于编程语言的东西(正如一些评论员所建议的那样)。我将添加更多信息以使这更清楚。首先,让我们列出当前的新行变体(例如,它们自 1999 年以来的变化):

  • \r\nis only used on Windows Notepad, the DOS command line, most of the Windows API and in some (older) Windows apps.
  • \nis used for all other systems, applications and the Internet.
  • \r\n仅用于 Windows 记事本、DOS 命令行、大多数 Windows API 和一些(较旧的)Windows 应用程序。
  • \n用于所有其他系统、应用程序和 Internet。

You'll notice that I've put most Windows apps in the \ngroup which may be slightly controversial but before you disagree with this statement, please grab a UNIX formatted text file and try it in 10 web friendly Windows applications of your choice (which aren't listed in my exceptions above). What percentage of them handled it just fine? You'll find that they (practically) all implement auto detection of line endings or just use \nbecause, while Windows may use \r\n, the Internet uses \n. Therefore, it is best practice for applications to use \nalone if you want your output to be Internet friendly.

您会注意到,我已将大多数 Windows 应用程序放在该\n组中,这可能会引起一些争议,但在您不同意此声明之前,请获取一个 UNIX 格式的文本文件,并在您选择的 10 个 Web 友好的 Windows 应用程序中尝试它(它们不是'没有在我上面的例外中列出)。他们中有多少人处理得很好?你会发现,他们(几乎)都实现行结尾的自动检测或刚使用\n,因为,而Windows可以使用\r\n,互联网的用途\n。因此,\n如果您希望输出对 Internet 友好,最好让应用程序单独使用。

PHP also defines a newline character called PHP_EOL. This constant is set to the OS specific newline string for the machine PHP is running on (\r\nfor Windows and \nfor everything else). This constant is not very useful for webpages and should be avoided for HTML output or for writing most text to files. It becomes VERY useful when we move to command line output from PHP applications because it will allow your application to output to a terminal Window in a consistent manner across all supported OSes.

PHP 还定义了一个名为PHP_EOL. 这个常量被设置为运行 PHP 的机器的操作系统特定的换行符字符串(\r\n对于 Windows 和\n其他所有东西)。这个常量对网页不是很有用,应该避免用于 HTML 输出或将大多数文本写入文件。当我们从 PHP 应用程序转向命令行输出时,它变得非常有用,因为它允许您的应用程序在所有支持的操作系统中以一致的方式输出到终端窗口。

If you want your PHP applications to work from any server they are placed on, the two biggest things to remember are that you should always just use \nunless it is terminal output (in which case you use PHP_EOL) andyou should also ALWAYS use /for your path separator (not \).

如果您希望您的 PHP 应用程序在它们所在的任何服务器上工作,要记住的两件最重要的事情是,\n除非它是终端输出(在这种情况下您使用 PHP_EOL)否则您应该始终使用它,并且您还应该始终将其/用于您的路径分隔符(不是\)。

The even longer explanation:

更长的解释:

An application may choose to use whatever line endings it likes regardless of the default OS line ending style. If I want my text editor to print a newline every time it encounters a period that is no harder than using the \nto represent a newline because I'm interpreting the text as I display it anyway. IOW, I'm fiddling around with measuring the width of each character so it knows where to display the next so it is very simple to add a statement saying that if the current char is a period then perform a newline action (or if it is a \nthen display a period).

应用程序可以选择使用它喜欢的任何行尾,而不管默认的 OS 行尾样式。如果我希望我的文本编辑器每次遇到一个句点时都打印一个换行符,这并不比使用\n表示换行符更难,因为我在解释文本时无论如何都要显示它。IOW,我正在摆弄测量每个字符的宽度,以便它知道在哪里显示下一个字符,因此添加一个语句非常简单,说明如果当前字符是一个句点,则执行换行操作(或者如果它是a\n然后显示一个句点)。

Aside from the null terminator, no character code is sacred and when you write a text editor or viewer you are in charge of translating the bits in your file into glyphs (or carriage returns) on the screen. The only thing that distinguishes a control character such as the newline from other characters is that most font sets don't include them (meaning they don't have a visual representation available).

除了空终止符之外,没有任何字符代码是神圣的,当您编写文本编辑器或查看器时,您负责将文件中的位转换为屏幕上的字形(或回车)。将换行符等控制字符与其他字符区分开来的唯一因素是大多数字体集不包含它们(这意味着它们没有可用的视觉表示)。

That being said, if you are working at a higher level of abstraction then you probably aren't making your own textbox controls. If this is the case then you're stuck with whatever line ending that control makes available to you. Even in this case it is a simple matter to automatically detect the line ending style of any string and make the conversion before you load your text into the control and then undo it when you read from that control. Meaning, that if you're a desktop application dev and your application doesn't recognize \nas a newline then it isn't a very friendly application and you really have no excuse because it isn't hard to make it the right way. It also means that whomever wrote Notepad should be ashamed of himself because it really is very easy to do much better and so many people suffer through using it every day.

话虽如此,如果您在更高的抽象级别上工作,那么您可能不会制作自己的文本框控件。如果是这种情况,那么您就会被该控件提供给您的任何行结尾所困扰。即使在这种情况下,自动检测任何字符串的行尾样式并在将文本加载到控件之前进行转换,然后在从该控件读取时撤消它也是一件简单的事情。意思是,如果您是桌面应用程序开发人员并且您的应用程序无法识别\n作为换行符,它不是一个非常友好的应用程序,你真的没有任何借口,因为它并不难以正确的方式实现。这也意味着写记事本的人应该为自己感到羞耻,因为它真的很容易做得更好,而且每天都有很多人在使用它。

回答by gahooa

If you are programming in PHP, it is useful to split lines by \nand then trim()each line (provided you don't care about whitespace) to give you a "clean" line regardless.

如果您使用 PHP 进行编程,那么将行拆分\n然后trim()每行(假设您不关心空格)以便为您提供“干净”的行是很有用的。

foreach($line in explode("\n", $data))
{
    $line = trim($line);
    ...
}

回答by D.R.