Hello,
I think this is a pretty common problem, for example, if we want to extract from a string with HTML code, all the strings that are between 2 certain tags.
Let's say:
$text = "blah blah<b>this is what I want 1</b>yada yada<b>this is waht I want 2</b> and so on..."
And I want to extract the bold things, strings between $start="<b>" and $end="</b>".
We can suppose there are no nested tags.
I've searched in the site and I've found similar problems, but not the solution to this.
I can do a eregi with "$start(.*)$end" to get rid of everything that is before and after the first <b> and </b>, but I can't figure out how to get all the substrings I want.
I'm using PHP3, so I cannot use the Perl-like regexps available in PHP4.
Of course, I can do a brute-force approach and produce the worst algorithm using many explodes or splits:
function LazyExtract($start,$end,$text) {
$ExpStart = explode($start,$text);
for ($i=0 ; $i < sizeof($ExpStart) ; $i++) {
$ExpEnd = explode($end,$ExpStart[$i]);
for ($j=1 ; $j < sizeof($ExpEnd); $j++) {
$Result[$i] = $ExpEnd[0];
}
}
return $Result;
}
That code is terrible, I'm using a nested loop that will grow as a square.
Any help appreciated.