One question I get a lot is “how do I explode a string into lines?”. The “default” approach, not necessarily bad, is the following:

$lines = explode( "\n", $string );

This is pretty simple and self explanatory. Use PHP’s explode() function and pass the newline \n character as a delimiter. This should work and yet, in some “strange” cases, it does not. First of all, the newline character or sequence of characters differs from OS to OS. On Microsoft operating systems, the newline is represented by a sequence of \r\n (carriage return – ASCII 10 & line feed – ASCII 13). On older Mac Operating Systems the newline char is \r (carriage return) while on *nix systems is \n. So the correct way to break a string into lines is to use all three of them as delimiters. The easiest way is with regular expressions and preg_split().

$lines = preg_split( '/\r\n|\r|\n/', $string );

If you have enough free time on your hands, you can try strtok or other, more exotic, methods.