Hi,

I've got a long string that I've read from a file or URL.
How can I extract intro a table all the text that is between two delimiters? (which would be 2 strings)

for example:
Let's say the string is "bla bla bla bla start text1 end bla bla start text2 end bla bla start text3 end bla bla"
What I need is get all the bits in that string that are between the delimiters "start" and "end". (given that those delimiters could be any strings)
I need something that returns:
$result[0] : "text1"
$result[1] : "text2"
$result[2] : "text3"

I'm trying to find something with regular expression, using preg_match_all, but can't get it right...

thanks a lot!

    For your example, you are using words like 'start' and 'end' as de-limiters. Is this really the case, or just for explanation?

    Using words to de-limit does makes it MUCH harder, so if you have the choice, it's better to use characters that definitely won't show up in the text you are searching.

    The standard regex pattern for de-limiters works like:

    "opening de-limiter" + "NOT closing de-limiter" + "closing de-limiter"

    So, if you were using "<" and ">" as de-limiters then the pattern would be:

    /<([>]+)>/

    // Basic use
    preg_match("/<([>]+)>/", $text, $matches);
    echo "matched text is: ".$matches[1]";

      Can't you replace your start and endchars with " start " and " end ", where start and end are the start and endwords. Notice the space before and after to indicate separate words.

        Well the start and end delimiter could be any string.
        So I need something that return whatever is between 2 given delimiters (I'm not choosing those delimiters, and they will be different everytime)

        For example, I could need anything between "<a href=" and "</a><p class="
        or anything between "pg" and "</font>"
        etc...

        So yes, I guess it's more complex than just specific characters. Thanks anyway for the reply!

          I wrote a function to do that... I'd appreciate it if you make sure to put a comment in there giving me credit if it helps you!

          http://www.durdenwebapplications.com/durdenwebapplications_PHP/#StrBetweenStr

          http://www.durdenwebapplications.com/durdenwebapplications_PHP/Func/StrBetweenStr.func.phps

          First var is the entire string, second var is the start delimeter, third var is the stop delimeter. Fourth var is OPTIONAL and is the number of results you want returned ( returns all matches if you don't give it this option )

          It returns an array of matches.

          $array = StrBetweenStr ($string, "start", "end");
          print_r ($array);

            Thanks, but I think I found the solution with regular expressions:

            $startString="whatever";
            $endString="whatever again";
            preg_match_all ("|$startString(.*)$endString|U", $bigString, $output, PREG_PATTERN_ORDER);

            and then:

            foreach ($output[1] as $item)
            echo $item."<br>";

            It works as long as you're careful to escape any suspicious character!

              Write a Reply...