PDA

View Full Version : [SOLVED] string NOT included in pattern (Regular expressions, PHP)



yc2
November 5th, 2010, 02:40 AM
Hello,

I use preg_replace, PHP5.

I wish an entire string NOT to be present in the search pattern. For example, I wish this:

xxxmango-sourxxxbanana-sourxxxlemon-sourxxxpapaya-sour

to be changed into this:

xxxmango-sweetxxxbanana-sweetxxxlemon-sourxxxpapaya-sweetxxx

i.e.
The pattern xxx[any word]-sour should be changed into xxx[any word]-sweet except where [any word]=lemon

My original problem regards parsing urls and not fruit ;)

Can someone please give the corresponding preg_replace command or similar.

Thanks.

I read this, but I just couldn't get it work
http://stackoverflow.com/questions/1172873/regex-to-match-a-pattern-but-exclude-a-set-of-words

yc2
November 5th, 2010, 05:58 AM
Finally got it, by the help of this page:
http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html#10

$string = "xxxmango-sourxxxbanana-sourxxxlemon-sourxxxpapaya-sour";
echo preg_replace("/(?<!lemon)-sour/", "\\1-sweet", $string);
// echos: xxxmango-sweetxxxbanana-sweetxxxlemon-sourxxxpapaya-sweet