\n vs. PHP_EOL vs.
?

后端 未结 6 1859
粉色の甜心
粉色の甜心 2020-12-04 18:04

In most cases, as for one interactive website, when we output multiple lines of contents to web client browser, in my opinion,
is much more prefer

6条回答
  •  無奈伤痛
    2020-12-04 18:28


    It was created for HTML designing...
    will not work in many places until it supports content type is

    an example1 in mail functions where the content type is text...
    will not work but /n will work.

    more examples where you will see different results.

    example 2

    
    
    
     
    
    

    As document.write writes to the DOM you need to use
    for a newline. Relevant MDN docs

    example3

    txt = "Hello \n World!" ; 
    alert(txt);
    

    because alert function is asking a message to be shown to the user it isn't the same as a popup window with HTML in it

    /n

    is basically common in most of the programming languages for a line break and it will work within most of the platforms. \n and PHP_EOL are actual, source code linebreaks.

    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:

    example

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

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

    Beware that old Macs use \r, this would be a better solution: preg_replace('~\r\n?~', "\n", $str);

    PHP_EOL

    should be used when writing output such as log files.

    It will produce the line break specific to your platform.

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

提交回复
热议问题