Php – Explode PHP string by new line

PHP

Simple, right? Well, this isn't working :-\

$skuList = explode('\n\r', $_POST['skuList']);

Best Answer

Best Practice

As mentioned in the comment to the first answer, the best practice is to use the PHP constant PHP_EOL which represents the current system's EOL (End Of Line).

$skuList = explode(PHP_EOL, $_POST['skuList']);

PHP provides a lot of other very useful constants that you can use to make your code system independent, see this link to find useful and system independent directory constants.

Warning

These constants make your page system independent, but you might run into problems when moving from one system to another when you use the constants with data stored on another system. The new system's constants might be different from the previous system's and the stored data might not work anymore. So completely parse your data before storing it to remove any system dependent parts.

UPDATE

Andreas' comment made me realize that the 'Best Practice' solution I present here does not apply to the described use-case: the server's EOL (PHP) does not have anything to do with the EOL the browser (any OS) is using, but that (the browser) is where the string is coming from.

So please use the solution from @Alin_Purcaru (three down) to cover all your bases (and upvote his answer):

$skuList = preg_split('/\r\n|\r|\n/', $_POST['skuList']);