php explode by multiple delimiters
April 19th, 2011
You need to explode a string by multiple delimiters?
You need to use the preg_split function. Preg_split is a function similar to explode.
Explode splits a string by a string, Preg_split splits a string by a regular expression.
Example
You have a text and you need to explode it by two strings, “with” and “and”.
$string = "One with two and three";
$array = preg_split("( with | and )", $string);
print_r($array);
Outputs:
Array ( [0] = One [1] = two [2] = three )
Wow, thats a really clever way of tinhikng about it!