Re: Get text between A and B?
From: Justin Koivisto (spam_at_koivi.com)
Date: 11/17/03
- Next message: Default User: "Re: learning php"
- Previous message: Utopian [ FTa ]: "Re: Workshop {Flash <-> PHP <-> MYSQL}"
- In reply to: Philipp Lenssen: "Get text between A and B?"
- Next in thread: Philipp Lenssen: "Re: Get text between A and B?"
- Reply: Philipp Lenssen: "Re: Get text between A and B?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 17 Nov 2003 19:52:09 GMT
Philipp Lenssen wrote:
> I want to read out several strings from an HTML (text) file, like
> everything between "<h2>" and "</h2>" to create a table-of-contents
> (also, things other than tags).
> I do have a function but it's slow, and sometimes doesn't finish for
> larger files (600K, not much really!).
> Now what would be a nice function to do this job? I suppose some regex
> with preg_match_all?
>
> 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"
>
> ------------
>
> Any help greatly appreciated!
>
Kinda like this then...
function getTextBetween($allText,$textBefore,$textAfter,$offset=0){
$pattern='#'.$textBefore.'(.*)'.$textAfter.'#iU';
preg_match_all($pattern, $allText,$matches);
return $matches[1][$offset];
}
--
Justin Koivisto - spam@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
- Next message: Default User: "Re: learning php"
- Previous message: Utopian [ FTa ]: "Re: Workshop {Flash <-> PHP <-> MYSQL}"
- In reply to: Philipp Lenssen: "Get text between A and B?"
- Next in thread: Philipp Lenssen: "Re: Get text between A and B?"
- Reply: Philipp Lenssen: "Re: Get text between A and B?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|