I find that sometimes I need to get some string which is between 2 so called delimiters. I don’t think PHP has a built in function to do this, so I was searching around for a solution and found a simple function which does exactly what I am looking for:
function getStringBetween($content,$start,$end)
{
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return false;
}
So the function does simply what it is named to do, it gets the FIRST instance of the string which is between $start and $end within the given haystack $content. In the event of no instances found, the function simply returns false.
After the above function exists, you can replace text in between 2 strings relatively easily, an easy way to go about this would be something along the lines of:
public static function replacebetween($content,$start,$end, $value)
{
$original_value = getbetween($content,$start,$end);
return str_replace($original_value,$value,$content);
}
Note though this is only a very rough and simple implementation, it can be easily made better.
Hope this will hope someone out there, and no I did not write the function, but I can’t remember who did, if you did, please let me know and I will credit you

Powerball Record: 13,014 RPM
October 8, 2008 at 4:00 pm
It would be nice if it returned an array of all the substrings between these start and end delimiters e.g.
$array = getStringsBetween($content,$start,$end,$maxcount)
This would be similar to the format for explode() and could be called explode_between()
October 9, 2008 at 11:05 am
How about this re-entrant function:
function explode_between($content, $start, $end, $max) {
if ((isset($max)) && ($max == 0)) {return FALSE;}
if (isset($max)) {$max– ;}
@$temparray = explode($start,$content,2);
if (count($temparray) < 2) {return FALSE;}
@$temparray = explode($end,$temparray[1],2);
if (count($temparray) < 2) {return FALSE;}
$returned = explode_between($temparray[1], $start, $end, $max);
if ($returned === FALSE) {$outarray = array();}
else {$outarray = $returned;}
array_unshift($outarray,$temparray[0]);
return $outarray;
}
October 9, 2008 at 11:07 am
********************************
function explode_between($content, $start, $end, $max) {
if ((isset($max)) && ($max == 0)) {return FALSE;}
if (isset($max)) {$max–- ;}
@$temparray = explode($start,$content,2);
if (count($temparray) < 2) {return FALSE;}
@$temparray = explode($end,$temparray[1],2);
if (count($temparray) < 2) {return FALSE;}
$returned = explode_between($temparray[1], $start, $end, $max);
if ($returned === FALSE) {$outarray = array();}
else {$outarray = $returned;}
array_unshift($outarray,$temparray[0]);
return $outarray;
};
October 9, 2008 at 7:54 pm
Good idea Nick, in my case above I only needed the first instance, but I like your approach which is a more general implementation.
I am yet to try your code, will let you know how I get on.
If your function works well I will add it to my post
thanks