Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explode a string with line break [duplicate]

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

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

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

Here is what I have so far:

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

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

array (size=1)
  0 => string '\r\n                         ( G  A  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?

like image 784
Ara Sivaneswaran Avatar asked Nov 28 '22 21:11

Ara Sivaneswaran


2 Answers

You can try to split the string with a regular expression. There is a class for newline characters:

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

Edit: Add missing delimiters to the regex and argument to function.

like image 153
Lucas Kahlert Avatar answered Dec 03 '22 17:12

Lucas Kahlert


The problem is that \r\n in the original text are NOT end of line symbols - it's just literally 'backslash-r-backslash-n' sequence. Hence you need to take this into account:

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

... i.e., use single quotes to delimit the string.

like image 45
raina77ow Avatar answered Dec 03 '22 17:12

raina77ow