0

This question is nearly identicle to : 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?

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for that big help. I'll run this awhile and come back and report how it does asap.
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?
@atwell, I've edit the answer to address that problem. You must make the quantifier non-greedy.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.