bluewalrus, it may be a good idea (as it usually is) to limit this:
explode($a,$b,2);
That returns (at most) two parts [before, after], rather than splitting it more if name|| is found again.
If you are certain that it will never be found more than once, then that is not necessary. It just makes things more predictable.
FSD, the approach above is probably what I'd write. The "best" way to do this would be to use regex if you can.
Here's a string-function only version (since explode is said to be slow):
Code:
$name = substr($name, strpos($name,$fineme)+strlen($findme),(strpos($name,$findme2,(strpos($name,$findme)+strlen($findme)))-(strpos($name,$findme)+strlen($findme))));
//I should really learn regex.
//but that was a good challenge
//note: untested... try it a few times before using it,
//or just use the much simpler explode version above
//this could also be a lot simpler if you split it onto a few lines:
$findme_end = strpos($name,$findme)+strlen($findme);
$findme2_start = strpos($name,$findme2,$findme_end);
$name = substr($name,$findme_end,$findme2_start-$findme_start);
//Also, if you have bad input this will give unexpected results. It may be best to check first that:
if (!strpos($name,$findme)||!strpos($name,$findme2,strpos($name,$findme)+strlen($findme))) {
//error
}
EDIT: Here's an interesting approach. This might actually be faster than all of the above and it's simpler in away:
Code:
$start = strpos($name,$findme)+strlen($findme);
$end = strpos($name,$findme2,$n);
$found = '';
$n=0;
while($n+$start<$end) { $found[$n] = $name[$n+$start]; $n++; }
$name = $found;
I realize that looks more complicated, but it's because "substr" is not a great tool in PHP for finding the string between two points.
You could make it prettier like this:
Code:
function stringbetween($string,$start,$end) {
$found = '';
$n=0;
while($n+$start<$end) { $found[$n] = $string[$n+$start]; $n++; }
return $found;
/////EDIT: ERROR IN THIS. SEE NEXT POST
}
$start = strpos($name,$findme)+strlen($findme);
$end = strpos($name,$findme2,$n);
$name = stringbetween($name,$start,$end);
stringbetween() should run somewhat faster than substr() for all of that.
(Sorry for giving too complex an answer here: I'm bored and trying to get used to coding with a new text editor.)
Bookmarks