如何使用PHP突出显示HTML输入文本中的搜索结果? [重复]

This question already has an answer here:

I have some input HTML and I need to highlight some words in it.
I saw many advices how to do it and now I use preg_replace.

 $output = preg_replace("/\w*?" . preg_quote($keyword) . "\w*/i",
        "<span class=\"entity_hilight\">$0</span>", $input);

It works good at all, but if the input HTML does not contain a keyword in some of attributes of tags.

For example if I need to highlight keyword "Hawaii", this HTML code will be broken after applying the code above:

Hawaii is on of the best places for fun and recreation. Welcome to <a href="http://example.com/Hawaii">Hawaii hotels</a>

Because the code will replace Hawaii not only in the <a> tag, but in the href attribute too.

So how to avoid breaking HTML code with highlighting? Please note that I need to highlight words inside and outside HTML tags both, also it should work properly with nested tags. Tags as well as attributes can have any, even custom names, so I can't explicitly define any list of possible tag and attribute names. Also sometimes keyword can match not only with attribute value, but with attribute name or tag name (for example if I would need higlight keyword "strong" or "article" or "class").

</div>