Preg Replace RegEx Question

Hello SP,

I’m trying to do the following:

	$words = array("/\\bred\\b/i", "/\\bgreen\\b/i", "/\\byellow\\b/i");
	$rplc = "black";
	$para = "The red dog was not happy today. The orange-green dog was very upset as well. The yellow dog was MAD! <a href=\\"green-yellow-dog.php\\">Green Dog</a>";
	echo preg_replace($words, $rplc, $para);

Although this works the way that I want it to on the content. I don’t want it to work on the urls with the hyphens. I want to retain the original links. Is there any easy way to work around if the word I’m searching for has any chars before or after it?

Thanks in advance,


Yes you can, using negative lookahead and negative lookbehind.
Too cryptic? read this article :slight_smile:

Nice! I’ll have to check this out. Thanks for the heads up!