php 我什么时候使用 PHP_EOL 而不是 \n ,反之亦然?Ajax/Jquery 客户端问题

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

When do I use PHP_EOL instead of \n and vice-versa ? Ajax/Jquery client problem

phpjqueryajaxpostline-breaks

提问by fabio

I have a php parser that split a given string by line-breaks, doing something like this:

我有一个 php 解析器,它通过换行符拆分给定的字符串,执行如下操作:

$lines = explode(PHP_EOL,$content);

The parser works fine when working on server side. However, when I pass the content via post by ajax (using jquery's $.post method) the problem arises: line breaks are not recogniezed. So after almost an hour of tests and head-aches I decided to changed PHP_EOL by "\n" and it worked:

在服务器端工作时,解析器工作正常。但是,当我通过 ajax(使用 jquery 的 $.post 方法)通过 post 传递内容时,问题出现了:无法识别换行符。因此,经过近一个小时的测试和头痛,我决定将 PHP_EOL 更改为 "\n" 并且它起作用了:

$lines = explode("\n",$content);

$lines =explode("\n",$content);

Now it works! Damn it I lost so much time! Could somebody explain me when use PHP_EOL and "\n" properly, so I can save time in the future? Appreciate your kind answers ;)

现在它起作用了!该死的我浪费了太多时间!有人可以解释一下什么时候正确使用 PHP_EOL 和 "\n" 以便我将来可以节省时间吗?感谢您的友好回答;)

回答by mario

The constant PHP_EOLshould generally be used for platform-specific output.

该常量PHP_EOL通常用于特定于平台的输出。

  • Mostly for file outputreally.
  • Actually the file functions already transform \n←→ \r\non Windows systems unless used in fopen(…, "wb")binary mode.
  • 主要用于文件输出
  • 实际上,除非在二进制模式下使用,否则文件函数已经在 Windows 系统上转换了\n←→ 。\r\nfopen(…, "wb")

For file inputyou should prefer \nhowever. While most network protocols (HTTP) are supposed to use \r\n, that's not guaranteed.

但是,对于文件输入,您应该更喜欢\n。虽然大多数网络协议 (HTTP) 都应该使用\r\n,但这并不能保证。

  • Therefore it's best to break up on \nand remove any optional \rmanually:

    $lines = array_map("rtrim", explode("\n", $content));
    

    Or use the file(…, FILE_IGNORE_NEW_LINES)function right away, to leave EOL handling to PHP or auto_detect_line_endings.

  • A more robust and terser alternative is using preg_split()and a regexp:

    $lines = preg_split("/\R/", $content);
    

    The \Rplaceholderdetects any combination of \r+ \n. So would be safest, and even work for Classic MacOS ≤ 9text files (rarely seen in practice).

    Obligatory microoptimization note:
    While regex has a cost, it's surprisingly often speedier than manual loops and string postprocessing in PHP.

  • 因此,最好手动拆分\n并删除任何可选项\r

    $lines = array_map("rtrim", explode("\n", $content));
    

    或者立即使用该file(…, FILE_IGNORE_NEW_LINES)函数,将 EOL 处理留给 PHP 或auto_detect_line_endings

  • 一个更健壮和更简洁的替代方案是使用preg_split()和一个正则表达式:

    $lines = preg_split("/\R/", $content);
    

    \R占位符检测的任何组合\r+ \n。所以最安全,甚至适用于经典 MacOS≤ 9文本文件(在实践中很少见)。

    强制性的微优化说明:
    虽然正则表达式有成本,但它通常比 PHP 中的手动循环和字符串后处理更快。

And there are a few classic examples where you should avoidPHP_EOLdue to its platform-ambiguity:

并且有一些经典示例,由于其平台歧义,您应该避免PHP_EOL

  • Manual generation of network protocol payloads, such as HTTP over fsockopen().
  • For mail()and MIME construction (which really, you shouldn't do tediously yourself anyway).
  • File output, ifyou want to consistentlywrite just Unix \nnewlines regardless of environment.
  • 手动生成网络协议负载,例如 HTTP over fsockopen().
  • 对于mail()和 MIME 构造(实际上,无论如何您都不应该自己做乏味的工作)。
  • 文件输出,如果您想在不考虑环境的情况下始终只编写 Unix\n换行符。

So use a literal "\r\n"combination when not writing to files, but preparing data for a specific context that expects network linebreaks.

因此,"\r\n"在不写入文件时使用文字组合,而是为需要网络换行符的特定上下文准备数据。

回答by alex

PHP_EOLshould be used when writing output such as log files.

PHP_EOL应该在写入日志文件等输出时使用。

It will produce the line break specific to your platform.

它将产生特定于您的平台的换行符。

回答by Jonah

PHP_EOLis 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.

PHP_EOL是保持服务器平台使用的换行符的常量。在 Windows 的情况下,它是\r\n. 在 *nix 上,它是\n. 您显然有一台 Windows 服务器。

If you were on a *nix server, that change wouldn't have fixed it, because it would be \n. If you are sending data to the client (i.e. the browser), you should use \r\nto ensure line breaks are recognized.

如果您在 *nix 服务器上,该更改不会修复它,因为它将是\n. 如果您要向客户端(即浏览器)发送数据,则应使用\r\n以确保识别换行符。

回答by mfonda

PHP_EOLis the line ending used by the server PHP is running on. User submitted content will probably have line ending in whatever format they use. However, instead of exploding on newlines, just using the file()function, it does exactly what you are after.

PHP_EOL是运行 PHP 的服务器所使用的行尾。用户提交的内容可能会以他们使用的任何格式结尾。但是,它不是在换行符上爆炸,而是使用该file()函数,它完全符合您的要求。

回答by O N

IMHO using PHP_EOL is preferable

恕我直言,最好使用 PHP_EOL

to ensure consistency between PHP and JS handling of line break, you may want to define end-of-line variable in JS using PHP_EOL

为确保 PHP 和 JS 处理换行符的一致性,您可能需要使用 PHP_EOL 在 JS 中定义行尾变量

var eol = '<?php echo str_replace(array("\n","\r"),array('\n','\r'),PHP_EOL) ?>';

afterwards, use eolfor splitting submitted textarea content

之后,eol用于拆分提交的textarea内容