Page 1 of 1

Need php parsing help (egrep?)

Posted: Thu Dec 18, 2003 12:44 pm
by Grant29
Hi,

I want to be able to extract a word out of a string. Here is an example:

Code: Select all

$teststring = "All I want want data ok ok";

$temp = ereg("want (.*) ok", $teststring, $content);
$temp = ereg_replace("<br>", "<br>", $content&#1111;1]);
$temp = ereg("want (.*) ok", $temp, $content);
$temp = ereg_replace("<br>", "<br>", $content&#1111;1]);
echo "Extracted data: $temp";
I want to extract the word "data". The example I posted works, but it's because I know that there are 2 "want" and "ok" substrings.

Assuming I don't know that the word is "data", how can I grab any text between the last "want" and the first "ok"?

Thanks,
Grant

Posted: Thu Dec 18, 2003 2:16 pm
by Weirdan

Code: Select all

$teststring = "All I want want data ok ok";
preg_match("/want(?! want) (.*) (?<!ok )ok/",$teststring,$matches);
echo $matches[1]."\n";