PHP Split String by Comma and New Line Characters

Category: 
Tags: 

Use this handy PHP snippet to split a string by commas and new line characters.

// Replace all the new lines with commas, then split (explode) by comma to get each part of the string.
$input = $my_string_with_commas_and_new_lines;
$input = preg_replace("/((\r?\n)|(\r\n?))/", ',', $input);
$pieces = explode(',', $input);
foreach($pieces as $part) {
  print($part);
}

There you have it, tride and true, just for you.

Add new comment