The thing I want to achieve with the code below: match a specified word case-insensitive and only once in a text and replace it with a link.
I have the following preg_match to match the word 'foo' in a string:
if (preg_match("/\bfoo\b/i", $text, $results, PREG_OFFSET_CAPTURE)) {
// substr_replace the word 'foo' for a link <a href..
}
No problem for a text without HTML, but imagine the following text with HTML:
Lorem ipsum dolor sit amet, <a href="/foo-bar/" title="foo bar">some other foo link</a> consectetur adipiscing elit foo bar.
In this case there will be a new link within the current link, because there will be a match with foo in the href-part (same problem for the title and name part).
How can the pattern be changed to match only the 'foo' outside a HTML statement?