开发者

str_ireplace does not work with non ASCII charecters

I need to highlight search result in text in Russian, but str_ireplace() just does not work when query has different case. I've tried everything that I've fould in the manual, but nothing works. Here what I have tried:

<?php
setlocale (LC_ALL, 'ru_RU');
$query = 'ПрОбЛеМа';
$result = 'Эта проблема нам не знакома.';

$result = str_ireplace($query, "<strong>$query</strong>", $result); // does not work
$result = preg_replace("/($query)/i", '<strong>$1</strong>', $result); // does not work
$result = mb_eregi_replace("$query", "<strong>$query</strong>", $result); // does not work
$result = ext_str_ireplace($query, "<strong>$query</strong>", $result); // from php.net - does not work
$result = highlightStr($result, $query); // from php.net - does not work

?>

Is there 开发者_运维技巧any way to make it work? I'm getting desperate here.

PHP 5.3.3


If you add the "u" (unicode) modifier to preg_replace, it should work:

$result = preg_replace("/($query)/ui", '<strong>$1</strong>', $result);


you can use mb_eregi_replace, if you use non ASCII/multi Byte

but remove the "

$result = mb_eregi_replace($query, "<strong>$query</strong>", $result);

you also can set your encoding:

mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");


Drop in replacement:

function mb_str_ireplace ($search, $replace, $subject, &$replacements) 
{
    return preg_replace("/$search/ui", $replace, $subject, -1, $replacements);
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜