Can I get away with preg_replace() here or do I need to use preg_split()?

huongvu91

New Member
I need to split (but not \[code\]preg_split()\[/code\]) my title on \[code\]/\[/code\], \[code\]:\[/code\] or whitespace, in that order. For example, if I split on \[code\]/\[/code\], then I don't want to bother looking for \[code\]:\[/code\] or whitespace.I thought I could do it with a regex (the input string here will never have HTML in it)\[code\]$delimiters = array('/', ':', '\s');$title = preg_replace('@(' . implode('|', $delimiters) . ')(.*)$@', '$1 <span>$2</span>', $title, 1);\[/code\]The regex I have will match the first space, and not bother with the others. This is not what I want.Obviously I could \[code\]strpos()\[/code\] for the other characters (\[code\]:\[/code\] and \[code\]/\[/code\]) and remove the \[code\]\s\[/code\] 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 \[code\]preg_split()\[/code\] here, and preserver the delimiter or can I do it with one regex?Thanks
 
Back
Top