Asked  8 Months ago    Answers:  5   Viewed   32 times

Reference: This is a self-answered question. It was meant to share the knowledge, Q&A style.

How do I detect the type of end of line character in PHP?

PS: I've been writing this code from scratch for too long now, so I decided to share it on SO, plus, I'm sure someone will find ways for improvement.


 * Detects the end-of-line character of a string.
 * @param string $str The string to check.
 * @param string $default Default EOL (if not detected).
 * @return string The detected EOL, or default one.
function detectEol($str, $default=''){
    static $eols = array(
        "x000D000A", // [UNICODE] CR+LF: CR (U+000D) followed by LF (U+000A)
        "x000A",     // [UNICODE] LF: Line Feed, U+000A
        "x000B",     // [UNICODE] VT: Vertical Tab, U+000B
        "x000C",     // [UNICODE] FF: Form Feed, U+000C
        "x000D",     // [UNICODE] CR: Carriage Return, U+000D
        "x0085",     // [UNICODE] NEL: Next Line, U+0085
        "x2028",     // [UNICODE] LS: Line Separator, U+2028
        "x2029",     // [UNICODE] PS: Paragraph Separator, U+2029
        "x0D0A",     // [ASCII] CR+LF: Windows, TOPS-10, RT-11, CP/M, MP/M, DOS, Atari TOS, OS/2, Symbian OS, Palm OS
        "x0A0D",     // [ASCII] LF+CR: BBC Acorn, RISC OS spooled text output.
        "x0A",       // [ASCII] LF: Multics, Unix, Unix-like, BeOS, Amiga, RISC OS
        "x0D",       // [ASCII] CR: Commodore 8-bit, BBC Acorn, TRS-80, Apple II, Mac OS <=v9, OS-9
        "x1E",       // [ASCII] RS: QNX (pre-POSIX)
        //"x76",       // [?????] NEWLINE: ZX80, ZX81 [DEPRECATED]
        "x15",       // [EBCDEIC] NEL: OS/390, OS/400
    $cur_cnt = 0;
    $cur_eol = $default;
    foreach($eols as $eol){
        if(($count = substr_count($str, $eol)) > $cur_cnt){
            $cur_cnt = $count;
            $cur_eol = $eol;
    return $cur_eol;


  • Needs to check encoding type
  • Needs to somehow know that we may be on an exotic system like ZX8x (since ASCII x76 is a regular letter) @radu raised a good point, in my case, it's not worth the effort to handle ZX8x systems nicely.
  • Should I split the function into two? mb_detect_eol() (multibyte) and detect_eol()
Wednesday, March 31, 2021
answered 8 Months ago

By using



     // The request is using the POST method

For more details please see the documentation for the $_SERVER variable.

Wednesday, March 31, 2021
answered 8 Months ago

Use nl2br() to turn the new lines into HTML <br/> elements:


echo nl2br("Hello World!nSecond line");

Wednesday, March 31, 2021
answered 8 Months ago
  • n is a Linux/Unix line break.
  • r is a classic Mac OS (non-OS X) line break. Mac OS X uses the above unix n.
  • rn is a Windows line break.

I usually just use n on our Linux systems and most Windows apps deal with it ok anyway.

Wednesday, June 9, 2021
answered 5 Months ago

Have you looked into this PEAR package?

Saturday, June 26, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :