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
Not sure what your problem is stemming from, but I just put together this little test case:
<?php
$uc = "SREČA";
mb_internal_encoding('utf-8');
echo $uc."\n";
$lc = mb_strtolower($uc);
echo $lc."\n";
echo preg_replace("/\b(".preg_quote($uc).")\b/ui", "<span class='test'>$1</span>", "test:".$lc." end test");
It's output on my machine:
SREČA
sreča
test:<span class='test'>sreča</span> end test
Seems to be working properly?
I feel really stupid right about now but the problem wasn't with Preg_* functions at all. I don't know why but I first checked if the given term is even in the string with StriPos
and since that function is not multi-byte safe it returned false
if the case of the text was not the same as the search term, so the Preg_Replace
wasn't even called.
So the lesson to be learned here is that always use multi-byte versions of functions if you have UTF8 strings.
If I'm not mistaken, preg_match
uses the current locale. Try setting the locale to the language which these characters belongs to. You probably need a utf8 based locale too. If you have mixed languages in your page, you may be able to find a generic international locale that works.
See also: http://www.phpwact.org/php/i18n/utf-8