Re: Get text between A and B?

From: Matty (matt+nntp_at_askmenoquestions.co.uk)
Date: 11/17/03


Date: Mon, 17 Nov 2003 21:08:12 +0000

Philipp Lenssen wrote:

> It should have a parameter telling which occurrence of the string
> should be used, e.g. the second, third and so on.
>
> ------------
>
> Like:
>
> function getTextBetween($allText, $textBefore, $textAfter, $offset = 0)
> {
> // ?
> }
>
> Then I could say:
>
> $s = getTextBetween("<h2>foo</h2><p>Hello World</p><h2>bar</h2>",
> "<h2>", "</h2>", 1);
> echo $s; // ... would be "bar"
>

function getTextDelims($alltext, $opener, $closer)
{
        preg_match_all('/'.preg_quote($opener).'(.+)'.preg_quote($closer).'/mU', $alltext, $allmatches);
        if ((count($allmatches) > 0) and (array_key_exists(1, $allmatches)))

        { return $allamtches[1]; }
        else { return array(); }
}

Then doing
$myanswers = getTextDelims(.....

$myanswers[1] contains offset 1, etc

If you want to make the matches case-insensitive, change '/mU' to '/mUi'

HTH

Matt



Relevant Pages

  • Re: handles in VBA
    ... > But, speaking of market size, hth does Soma get away with a number like 2.5 million? ... > I'm betting it's far closer to 1/10th that size! ...
    (microsoft.public.office.developer.vba)
  • Re: handles in VBA
    ... > But, speaking of market size, hth does Soma get away with a number like 2.5 million? ... > I'm betting it's far closer to 1/10th that size! ...
    (microsoft.public.vb.winapi)
  • Re: setting default line spacing (single space) for envelopes
    ... Automatically Update. ... If that isn't the problem we'll take a closer look:-) ... HTH |:>) ... Bob Jones ...
    (microsoft.public.mac.office)
  • Re: METS BELIEVE IN SANTA CLOSE
    ... Matt wrote: ... "closer" is, in my opinion, a matter of debate. ... If Heilman (random ... We need a closer and we need relievers who can do the job. ...
    (alt.sports.baseball.ny-mets)
  • Re: METS BELIEVE IN SANTA CLOSE
    ... Matt wrote: ... "closer" is, in my opinion, a matter of debate. ... If Heilman (random ... We need a closer and we need relievers who can do the job. ...
    (alt.sports.baseball.ny-mets)