Question

I already did a search on Google and Stackoverflow, but I couldn't find any solution that works for me.

This is what I have so far:

$string = preg_replace('/'.$keyword.'/i', 
'<span class="highlight">$0</span>', $string);

Which works fine, except when the string contains anchor tags. But I still want to be able to highlight the keywords outside and within the anchor tags.

Example:

$keyword = 's';

Output:

I alrady did a search on Google and Stackoverflow, but I couldn't find any solution that works for me.

I would appreciate it if someone could find a solution for this without having to use PHP Simple HTML DOM Parser.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top