How can I put strings in an array, split by new line?

Johan picture Johan · Sep 27, 2009 · Viewed 362.4k times · Source

I have a string with line breaks in my database. I want to convert that string into an array, and for every new line, jump one index place in the array.

If the string is:

My text1
My text2
My text3

The result I want is this:

Array
(
    [0] => My text1
    [1] => My text2
    [2] => My text3
)

Answer

David picture David · Jun 23, 2012

I've always used this with great success:

$array = preg_split("/\r\n|\n|\r/", $string);

(updated with the final \r, thanks @LobsterMan)