什么时候使用PHP_EOL,而不是\ n和反之亦然?阿贾克斯/ jQuery的客户端问题 [英] When do I use PHP_EOL instead of \n and vice-versa ? Ajax/Jquery client problem

查看:178
本文介绍了什么时候使用PHP_EOL,而不是\ n和反之亦然?阿贾克斯/ jQuery的客户端问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP解析器,由换行符分割给定的字符串,做这样的事情:

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

$lines = explode(PHP_EOL,$content);

在服务器端工作时的解析器工作正常。然而,当我通过AJAX传递内容通过后(使用jQuery的$。员额法)的问题出现了:换行不recogniezed。所以,经过近一个小时的测试和头部疼痛,我决定改变PHP_EOL由\ N和它的工作:

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:

$行=爆炸(\ N,$内容);

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

现在它的作品!该死的我失去了这么多时间!可能有人解释我在使用时PHP_EOL和\ N的正确,这样我就可以节省时间的未来?鸭preciate你一种答案;)

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 ;)

推荐答案

恒<一href="http://pear.php.net/reference/PHP_Compat-latest/__filesource/fsource_PHP_Compat__PHP_Compat-1.6.0a2CompatConstantPHP_EOL.php.html"相对=nofollow> PHP_EOL 一般应使用特定平台的输出。

The constant PHP_EOL should generally be used for platform-specific output.

  • 对于大多的文件输出的真。
  • 其实文件的功能已经变换←→ \ r \ñ在Windows系统上,除非用在的fopen(...,世行)二进制模式。
  • Mostly for file output really.
  • Actually the file functions already transform \n ←→ \r\n on Windows systems unless used in fopen(…, "wb") binary mode.

有关的文件输入的你应该preFER 但是。虽然大多数网络协议(HTTP),都应该使用 \ r \ñ,也不能保证。

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

  • 因此​​,最好向上突破,并删除任何可选 \ r 手动:

  • Therefore it's best to break up on \n and remove any optional \r manually:

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

或者使用 文件(...,FILE_IGNORE_NEW_LINES) 功能向右走,离开EOL处理,以PHP或 auto_detect_line_endings

一个更强大和更简洁的选择是使用 preg_split() 和正则表达式:

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

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

借助 \ r 占位符检测<大骨节病>任意组合\ r + <大骨节病> \ñ。所以,将是最安全的,甚至工作经典的MacOS ≤9 文本文件(很少见的做法)。

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

强制性microoptimization注:
虽然正则表达式是有成本的,这是令人惊讶的往往比人工循环和后处理字符串在PHP快。

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

这里面有几个经典的例子,你应该的避免 PHP_EOL 的,因为它的platform-ambiguity

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

  • 在手动代网络协议的有效载荷,如HTTP超过 的fsockopen()
  • 有关 电子邮件() 和MIME建设(其中真的,你不应该做的繁琐自己反正)。
  • 在文件输出的如果要的持续的仅编写Unix的换行,无论环境。
  • 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, if you want to consistently write just Unix \n newlines regardless of environment.

因此​​,使用文字\ r \ N组合时不能写入文件,但preparing数据为预期的网络换行符特定的上下文的。

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

这篇关于什么时候使用PHP_EOL,而不是\ n和反之亦然?阿贾克斯/ jQuery的客户端问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆