Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php str_ireplace不适用于非ASCII字符_Php_Regex_Search - Fatal编程技术网

Php str_ireplace不适用于非ASCII字符

Php str_ireplace不适用于非ASCII字符,php,regex,search,Php,Regex,Search,我需要突出显示俄语文本中的搜索结果,但str_ireplace()在查询有不同大小写时不起作用。我已经尝试了所有我在这所学校学到的东西,但是没有任何效果。以下是我尝试过的: <?php setlocale (LC_ALL, 'ru_RU'); $query = 'ПрОбЛеМа'; $result = 'Эта проблема нам не знакома.'; $result = str_ireplace($query, "<strong>$query</stro

我需要突出显示俄语文本中的搜索结果,但str_ireplace()在查询有不同大小写时不起作用。我已经尝试了所有我在这所学校学到的东西,但是没有任何效果。以下是我尝试过的:

<?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

?>

有什么办法使它起作用吗?我在这里越来越绝望了

PHP 5.3.3

如果将“u”(unicode)修饰符添加到preg_replace,则它应该可以工作:

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

如果使用非ASCII/多字节,则可以使用mb_eregi_replace

但是,请删除“

临时替代品:

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

天哪,这真是天才!我多么想为此给你买杯啤酒!猜得好,因为非ASCII并不自动意味着Unicode;-)@lvaro G.Vicario,我想我是在充满阳光的世界中长大的,在那里非ASCII默认意味着Unicode。更不用说我的错:)如果指针行中的字符寄存器不能更改(!)。
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
function mb_str_ireplace ($search, $replace, $subject, &$replacements) 
{
    return preg_replace("/$search/ui", $replace, $subject, -1, $replacements);
}