I'm enhancing our video search page to highlight the search term(s) in the results. Because user can enter judas priest
and a video has Judas Priest
in it's text I have to use regular expressions to preserve the case of the original text.
My code works, but I have problems with special characters like š, č and ž
, it seems that Preg_Replace()
will only match if the case is the same (despite the /ui
modifier).
My code:
$Content = Preg_Replace ( '/\b(' . $term . '?)\b/iu', '<span class="HighlightTerm">$1</span>', $Content );
I also tried this:
$Content = Mb_Eregi_Replace ( '\b(' . $term . '?)\b', '<span class="HighlightTerm">\\1</span>', $Content );
But it also doesn't work. It will match "SREČA" if the search term is "SREČA", but if the search term is "sreča" it will not match it (and vice versa).
So how do I make this work?
update: I set the locale and internal encoding:
Mb_Internal_Encoding ( 'UTF-8' );
$loc = "UTF-8";
putenv("LANG=$loc");
$loc = setlocale(LC_ALL, $loc);