tags:

views:

26

answers:

1

This question is nearly identicle to : http://stackoverflow.com/questions/2305362/php-search-string-with-wildcards, but will be arranged a little differently. I had a hard time understanding the proposed solutions to make my own out of them . Maybe you guys can help?

I have a start string and an end string which I use to sandwich strings of interest.

This is the function I'm currently using for this:

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

Sometimes my beginning strings need to have wildcards in them to account for a dynamic part of a template

for example I would need this to be my begin code variable:

$begin_code = "id='%wildcard%' class='sailors_assistant_%wildcard%'>";

and edit my original function above to detect %wildcard%(s) and account for them when using their sandwich search-n-grabs.

Any advise?

+2  A: 

The easiest way would be to use a regular expression:

function get_string_between($string, $start, $end) {
    $start = str_replace("%wildcard%", ".*?", preg_quote($start, "/");
    $end = str_replace("%wildcard%", ".*?", preg_quote($end, "/");
    $regex = "/$start(.*?)$end/";
    if (preg_match($regex, $string, $matches))
        return $matches[1];
    else
        return false;
}
Artefacto
@artefacto, Thanks for that big help. I'll run this awhile and come back and report how it does asap.
atwellpub
@art, Hello--- I've been running and playing with the function and ran into an issue. Issue is that when running this regex /$start(.*)$end/ on a large area of text that has more than one occurrence of $end, it will use the last occurrence rather than the most recent one found after $start. Is there a way around this?
atwellpub
@atwell, I've edit the answer to address that problem. You must make the quantifier non-greedy.
Artefacto
Fantastic! Final regex was $regex = "/$start(.*?)$end/si";, the s modified was necessary for the type of work I was doing, but I'm a little hazzy on the reasoning. The i modifier was for case insensitive, which is not really necessary.
atwellpub