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

一世执手 提交于 2019-12-17 09:31:41

问题


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

$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:

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


回答1:


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

  • Mostly for file output really.
  • Actually the file functions already transform \n ←→ \r\n on Windows systems unless used in fopen(…, "wb") binary mode.

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

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

    $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 \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).

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

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

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

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




回答2:


PHP_EOL should be used when writing output such as log files.

It will produce the line break specific to your platform.




回答3:


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

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\n to ensure line breaks are recognized.




回答4:


PHP_EOL is 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.




回答5:


IMHO using PHP_EOL is preferable

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

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

afterwards, use eol for splitting submitted textarea content



来源:https://stackoverflow.com/questions/4975411/when-do-i-use-php-eol-instead-of-n-and-vice-versa-ajax-jquery-client-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!