Need php parsing help (egrep?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Grant29
Forum Newbie
Posts: 2
Joined: Thu Dec 18, 2003 12:42 pm

Need php parsing help (egrep?)

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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";
Post Reply