$split_point = \' - \';
$string = \'this is my - string - and more\';
How can i make a split using the second instance of $split_point and not the
I've stumbled uppon the same, and fixed it like so:
$split_point = ' - ';
$string = 'this is my - string - and more';
$reverse_explode = array_reverse(explode($split_point, $string));
this is my - string - and more
Code
$arrSpits=explode("", "this is my - string - and more");
$arrSize=count($arrSpits);
echo "Last string".$arrSpits[arrSize-1];//Output: and more
array_pop(arrSpits); //now pop the last element from array
$firstString=implode("-", arrSpits);
echo "First String".firstString; //Output: this is my - string
Why not split on ' - ', but then join the first two array entries that you get back together?
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.
I liked Moff's answer, but I improved it by limiting the number of elements to 2 and re-reversing the array:
$split_point = ' - ';
$string = 'this is my - string - and more';
$result = array_reverse(array_map('strrev', explode($split_point, strrev($string),2)));
Then $result will be :
Array ( [0] => this is my - string [1] => and more )
Not sure why no one posted a working function with $limit
support though here it is for those who know that they'll be using this frequently:
<?php
function explode_right($boundary, $string, $limit)
{
return array_reverse(array_map('strrev', explode(strrev($boundary), strrev($string), $limit)));
}
$string = 'apple1orange1banana1cupuacu1cherimoya1mangosteen1durian';
echo $string.'<br /><pre>'.print_r(explode_right(1, $string, 3),1).'</pre>';
?>