I want to highlight a given text (needle) within another text (haystack). The issue here is: the haystack may also have HTML/XML-tags and numerous attributes in it. With "highlight" I mean to turn "keyword" into "<b>keyword</b>" for example. But this breaks everything when the haystack turns out to be something like
Hello, my name is <a href="email@example.com">mr. keyword</a>.
While substituting the second "keyword" is totally ok, the first one should NOT be recognized. It is like highlighting the keyword "IMG" - but only when it is a text, not when it is a tag like <IMG />.
Is it even possible with RegExp? Maybe by counting "<" and ">" somehow? I plan to use it in PHP (preg_replace).
Thanks in advance :)