Log in

View Full Version : using PCRE to replace everything between 2 words ungreedy.



james438
07-21-2007, 07:22 AM
like the long title says. How do you use PCRE to replace everything between two words like "have" and "not" in the following

$string = "the have and the havenots. Yet the haves are not there.";

so that $string becomes

"the s. Yet the there.";

as always, this is not really important, but I am curious :)
thanks.

PS is this the right place for posting PCRE questions? It is sort of PHP and sort of not. Maybe I should post these questions in the 'Other scripting/ coding related discussions.' forum?

Twey
07-21-2007, 01:20 PM
Good a forum as any... you'd do it like this:
$string = preg_replace('/have.*?not ?/', '', $string);

james438
07-21-2007, 06:51 PM
Awesome, thanks for the info :) I was scanning google and just not finding the answer anywhere.