Explode line with line break

I am trying to break a string into an array using the explode function.

I want it to break the line using line breaks found inside the line.

I looked through it and I tried all the ways, but I can't get it to work.

Here is what I still have:

$r = explode("\r\n" , $roster[0]); 

But when I var_dump the variable, I get the following:

 array (size=1) 0 => string '\r\n ( GA Sh Hi Ga Ta PIM, +\/- Icetime Rating)\r\nR Danny Kristo 1 0 2 0 0 0 0 1 7.00 7\r\nR Brian Gionta 1 1 5 1 1 0 0 0 19.20 8\r\nR Steve Quailer... 

Any ideas why?

+4
source share
3 answers

The problem is that \r\n in the source text is NOT a line terminator - it's just literally "backslash -r-backside -n". Therefore, you should consider this:

 $r = explode('\r\n', $roster[0]); 

... i.e. use single quotes to delimit a string.

+6
source

You can try breaking a string into a regular expression. There is a class for newline characters:

 $r = preg_split('/\R/', $string); 

Change Add missing separators to the regular expression and function argument.

+15
source

You can use system EOL

  $r = explode(PHP_EOL, $roster[0]); 
+2
source

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


All Articles