I have a word in a variable $word. With preg_replace () I want to find the occurrance of $word and replace it with <u>$word</u>. I'm using \b$word\b to find the word, but the problem is this finds occurrences of the word inside <a> tags. For example, if $word is "john", then I get this:
$string = "<a href='http://www.john.com'>some link</a>";
$word = "john";
$pattern = "/\b$word\b/i";
$replacement = "<u>\\0</u>";
$newString = preg_replace ( $pattern, $replacement, $string );
echo $newString;
// output: <a href='http://www.<u>john</u>.com'>some link</a>
I want to replace $word in all occurences except where it occurs inside an <a> tag. Anybody know how to do this with regex?