Re: Get text between A and B?
From: Matty (matt+nntp_at_askmenoquestions.co.uk)
Date: 11/17/03
- Next message: Disco Plumber: "Re: mysql/php query"
- Previous message: Disco Plumber: "Re: mysql/php query"
- In reply to: Philipp Lenssen: "Get text between A and B?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Disco Plumber: "Re: mysql/php query"
- Previous message: Disco Plumber: "Re: mysql/php query"
- In reply to: Philipp Lenssen: "Get text between A and B?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|