Get String between Some Strings – PHP

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 :-)

 


4 Responses to “Get String between Some Strings – PHP”

  1. Nick Says:

    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()

  2. Nick Says:

    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;
    }

  3. Nick Says:

    ********************************

    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;
    };

  4. pucadinga Says:

    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 :)


Leave a Reply