Preg_Replace and UTF8

I am improving our video search page page to highlight search terms in the results. As the user can enter judas priest, but the video has judas priest, in this text I have to use regular expressions to keep the source text.

My code works, but I have problems with special type characters Ŕ, č and ž, it seems that it Preg_Replace()will only match if the case is the same (despite the modifier /ui). My code is:

$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 does not work. It will match "SREČA" if the search query is "SREČA", but if the search query is "sreča", it will not match it (and vice versa).

So how do I do this job?

update: I set the locale and internal encoding:

Mb_Internal_Encoding ( 'UTF-8' );
$loc = "UTF-8";
putenv("LANG=$loc");
$loc = setlocale(LC_ALL, $loc);
+3
source share
3 answers

I feel really stupid now, but the problem was not in the functions of Preg_ *. I don’t know why, but I checked first whether the given term is even in line with StriPos, and since this function is not multibyte security, it returns falseif the text was not the same as a search query, therefore Preg_Replaceit was not even called.

So, the lesson to be learned here is that always use multi-byte versions of functions if you have UTF8 lines.

+6
source

, preg_match . , . , , utf8. , , .

: http://www.phpwact.org/php/i18n/utf-8

+2

Not sure what your problem is coming from, but I just put together this small 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 is displayed on my machine:

SREČA
sreča
test:<span class='test'>sreča</span> end test

Does it seem to work correctly?

+2
source

Source: https://habr.com/ru/post/1728431/


All Articles