How to explode only on the last occurring delimiter? How to explode only on the last occurring delimiter? arrays arrays

How to explode only on the last occurring delimiter?


You may use strrev to reverse the string, and then reverse the results back:

$split_point = ' - ';$string = 'this is my - string - and more';$result = array_map('strrev', explode($split_point, strrev($string)));

Not sure if this is the best solution though.


How about this:

$parts = explode($split_point, $string);$last = array_pop($parts);$item = array(implode($split_point, $parts), $last);

Not going to win any golf awards, but it shows intent and works well, I think.


Here is another way of doing it:

$split_point = ' - ';$string = 'this is my - string - and more';$stringpos = strrpos($string, $split_point, -1);$finalstr = substr($string,0,$stringpos);