Detect EOL type using PHP

后端 未结 6 816
南方客
南方客 2020-12-05 11:39

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

How do I detect the ty

6条回答
  •  情话喂你
    2020-12-05 12:16

    Wouldn't it be easier to just replace everything except new lines using regex?

    The dot matches a single character, without caring what that character is. The only exception are newline characters.

    With that in mind, we do some magic:

    $string = 'some string with new lines';
    $newlines = preg_replace('/.*/', '', $string);
    // $newlines is now filled with new lines, we only need one
    $newline = substr($newlines, 0, 1);
    

    Not sure if we can trust regex to do all this, but I don't have anything to test with.

    enter image description here

提交回复
热议问题