How about this?
/**
* Like explode with multiple delimiters.
* Default delimiters are: \ | / and ,
*
* @param string $string String that thould be converted to an array.
* @param mixed $delimiters Every single char will be interpreted as an delimiter. If a delimiter with multiple chars is needed, use an Array.
* @return array. If $string is empty OR not a string, return false
*/
public static function multiExplode($string, $delimiters = '\\|/,')
{
$delimiterArray = is_array($delimiters)?$delimiters:str_split($delimiters);
$newRegex = implode('|', array_map (function($delimiter) {return preg_quote($delimiter, '/');}, $delimiterArray));
return is_string($string) && !empty($string) ? array_map('trim', preg_split('/('.$newRegex.')/', $string, -1, PREG_SPLIT_NO_EMPTY)) : false;
}
In your case you should use an Array for the $delimiters param. Then it is possible to use multiple chars as one delimiter.
If you don't care about trailing spaces in your results, you can remove the array_map('trim', [...] )
part in the return row. (But don't be a quibbler in this case. Keep the preg_split
in it.)
Required PHP Version: 5.3.0 or higher.
You can test it here