I need to split (but not preg_split()
) my title on /
, :
or whitespace, in that order. For example, if I split on /
, then I don't want to bother looking for :
or whitespace.
I thought I could do it with a regex (the input string here will never have HTML in it)
$delimiters = array('/', ':', '\s');
$title = preg_replace('@(' . implode('|', $delimiters) . ')(.*)$@', '$1 <span>$2</span>', $title, 1);
The regex I have will match the first space, and not bother with the others. This is not what I want.
Obviously I could strpos()
for the other characters (:
and /
) and remove the \s
from the delimiters if it found the others. This will solve the first problem.
I also want to pick the furthest right match, i.e. if splitting a sentence on whitespace, I want the last word to be matched.
Do I need to use preg_split()
here, and preserver the delimiter or can I do it with one regex?
Thanks