PHP – parse a string between two strings

This is a handy little function to strip out a string between two specified pieces of text. This could be used to parse XML text, bbCode, or any other delimited code/text for that matter.


function get_string_between($string, $start, $end){
	$string = " ".$string;
	$ini = strpos($string,$start);
	if ($ini == 0) return "";
	$ini += strlen($start);   
	$len = strpos($string,$end,$ini) - $ini;
	return substr($string,$ini,$len);
}

$fullstring = "this is my [tag]dog[/tag]";
$parsed = get_string_between($fullstring, "[tag]", "[/tag]");

echo $parsed; // (result = dog)

Share

64 Responses to PHP – parse a string between two strings

  1. Breakaway says:

    Thanks… it worked awesome for me… I like that it worked for the same exact text, so I used ^^ for both the start and stop, and it worked out fine.

    Thank you so so so so much.

  2. David Parker says:

    Thank you so much! I have tried doing this myself but just got a headache, and your function is the only one that works for multiple things in one string that I've found. Thank you!!

  3. Dan Dunleavy says:

    Hey Justin

    Wanted to compliment you on your header graphic ..Nice!

    I wouldn't include the "complete waste of time" alternate text though.
    You never know who is coming here and how they will take it.
    Only my opinion.

    Here at your site looking at some PHP code. Thanks for the help.

    Dan

  4. Mat says:

    This is a optimized version supported no $end attribute (and then getting all content of the line:

    /// BEGIN CODE /////////////////////////////////////

    function GetBetween($content,$start,$end = ""){
    $r = explode($start, $content);
    if (isset($r[1])){
    if (!empty($end)){
    $r = explode($end, $r[1]);
    return $r[0];
    }else{
    return $r[1];
    }
    }
    return ";
    }

    /// END CODE ///////////////////////////////////////

    Regards,

    Mat

  5. Scott says:

    This is great for tag parsing; thanks. 🙂

  6. rob says:

    I could kiss you on the lips. (not really, but THANKS!!!)

  7. CooL LeO says:

    Thanks. this works like charm for me.

  8. darthmartin says:

    You rock!! Thank you, thank u, thanku!!

  9. Erik says:

    I was writing a php based search engine script for my site and wanted to:
    1. fetch the content of a page
    2. find an occurence of the searched string
    3. store the url and some text around the found string
    4. get all links on that page and
    5. loop throught these links and repeat steps 1-4.

    I wont write the whole script, but only the extended default function that Justin has put up. So here it is:

    – added a while loop which cycles throught the whole string (could be a whole html page)
    – added an limit var to control how many occurences I want to fetch
    – added an extra parameter $distinct which enables me to NOT store strings that allready have been stored in the results array – nice for duplicate URLs

    NOTE: I am sure this function could be improved so dont hesitate do to so 🙂

    To test it copy this code:

    function get_string_between($string, $start, $end, $limit, $distinct=true)
    {
    $result=array();
    $num_of_occurences=0;
    while(($ini = strpos($string,$start, $new_start))!==FALSE)
    {
    $ini += strlen($start);
    $new_start = strpos($string,$end,$ini);
    $len=$new_start – $ini;
    $new_start+= strlen($end);
    $found_string=substr($string,$ini,$len);

    if($distinct)
    {
    if(!in_array($found_string, $result))
    {
    $result[]=$found_string;
    $num_of_occurences++;
    }
    }
    else
    {
    $result[]=$found_string;
    $num_of_occurences++;
    }

    if($limit==$num_of_occurences && $limit!=0)
    break;
    }
    return $result;
    }

    $content = file_get_contents('http://www.selectif.si/');

    $result = get_string_between($content, "href=\"", "\"", 0, true);

    echo "";
    print_r($result);
    echo "";

  10. […] Because obviously I didn't want visitors to the site to see that endless string of code. Now, all I needed to do was cut out everything except my MLS #, store that to a new variable, and echo it! To do that, I pulled a well-worn custom function from my bag of tricks (tip of the cap to Mr. Justin Cook) […]

  11. Luis T says:

    Life saver man! Wheres the donate button!?!?

  12. bouma says:

    case insensitive version:

    function get_istring_between($string, $start, $end){
    $string = " ".$string;
    $ini = stripos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = stripos($string,$end,$ini) – $ini;
    return substr($string,$ini,$len);
    }

    $fullstring = "this is my [TAG]dog[/TAG]";
    $parsed = get_istring_between($fullstring, "[tag]", "[/tag]");

    echo $parsed; // (result = dog)

  13. Jason Fetterly says:

    Wouldn't it be slightly cleaner to use the following equivalent code rather than changing the string?

    function get_string_between($string, $start, $end){
    $ini = strpos($string, $start);
    if ($ini === false) return ";
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) – $ini;
    return substr($string, $ini, $len);
    }

    $fullstring = 'this is my [tag]dog[/tag]';
    $parsed = get_string_between($fullstring, '[tag]', '[/tag]');

    echo $parsed; // (result = dog)

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image