I love the SUBSTRING_INDEX function in MySQL, especially because you can use negative indexes to start searching from the right side of the string.
Is there an equiv
I was curious and tested another method using a preg/match setup, then refactored it to allow for any number of delimiters/count. I added in the count check the other example was using, but I would probably also recommend some kind of sanitization of the delimiter field.
function substring_index($subject, $delim, $count){
if($count < 0){
$notRe = '[^\\'.$delim.']*';
$elem = array();
for($x=1;$x<=$count;$x++){
array_push($elem,$notRe);
}
$re = '/^('.implode('\\'.$delim,$elem).')/';
preg_match($re, $subject,$m);
if(count($m) == 2) {
return $m[1];
}
}
}