Explode on new line works with "\ n", but not with PHP_EOL. What for?

I have this line

enter image description here

As you can see, it explicitly contains a new line. I want to split this message into an array of two messages, and I decided that I use it explode()in conjunction with PHP_EOL, to work with several OSs, but I was surprised since it did not work.

explode(PHP_EOL, $str); // Array( [0] => "Divakat has attacked Gergana, dealing 591 physical damage. ( 2 absorbed )
                        // Gergana has died." )

explode("\n", $str); // Array( [0] => ""Divakat has attacked Gergana, dealing 591 physical damage. ( 2 absorbed )", [1] => "Gergana has died." )

I would like to ask why the first example does not work as I expect, and is there a way to make it work, because I really would not want it to print a new line character there. Thank.

EDIT:

I forgot to mention that the message was created on the same computer as this code is currently being tested. No OS changes.

+4
source share
1 answer

PHP_EOL , . , , , .

, \n (Unix EOL hardcoded):

$str = "Hello\nWorld";

Unix, 2:

echo count(explode(PHP_EOL, $str));

1, $str Windows, Mac EOL.

: , ; . Unix EOL Windows, \n.

+4

Source: https://habr.com/ru/post/1545503/


All Articles