PHP Code

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)



  1. 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. 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. 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.


  4. 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];
    return $r[1];
    return ";

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



  5. This is great for tag parsing; thanks. 🙂

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

  7. Thanks. this works like charm for me.

  8. Simply Super BBBBBBBB

    Simply Super BBBBBBBB

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

  10. 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)
    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);

    if(!in_array($found_string, $result))

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

    $content = file_get_contents('');

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

    echo "";
    echo "";

  11. dandy plow weberies: lorem ipsum, what? et al...

    […] 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) […]

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

  13. 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)

  14. 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