突出显示多字节文本中的单词
<?php
$s = 'Алабала';
$f = 'а';
echo preg_replace('/('.$f.')/iu', '<b>$1</b>', $s);
?>
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
mb_eregi_replace — 忽略大小写并支持多字节的正则表达式替换
$pattern
,$replacement
,$string
,$options
= null
扫描 string
中与 pattern
匹配的内容,然后将匹配的文本替换为 replacement
。
pattern
正则表达式模式。可以使用多字节字符。将忽略大小写。
replacement
替换文本。
string
要搜索的 字符串。
options
版本 | 描述 |
---|---|
8.0.0 |
options 现在可以为空。 |
7.1.0 | 该函数检查 string 是否对当前编码有效。 |
7.1.0 | e 修饰符已被弃用。 |
注意:
内部编码或由 mb_regex_encoding() 指定的字符编码将用作此函数的字符编码。
在处理不受信任的输入时,切勿使用 e
修饰符。不会发生自动转义(如 preg_replace() 中所知)。如果不注意这一点,很可能会在您的应用程序中创建远程代码执行漏洞。
突出显示多字节文本中的单词
<?php
$s = 'Алабала';
$f = 'а';
echo preg_replace('/('.$f.')/iu', '<b>$1</b>', $s);
?>
用于 UTF 字符的西里尔字母到拉丁字母的音译器
<?php
function do_translit($st) {
$replacement = array(
"й"=>"i","ц"=>"c","у"=>"u","к"=>"k","е"=>"e","н"=>"n",
"г"=>"g","ш"=>"sh","щ"=>"sh","з"=>"z","х"=>"x","ъ"=>"\'",
"ф"=>"f","ы"=>"i","в"=>"v","а"=>"a","п"=>"p","р"=>"r",
"о"=>"o","л"=>"l","д"=>"d","ж"=>"zh","э"=>"ie","ё"=>"e",
"я"=>"ya","ч"=>"ch","с"=>"c","м"=>"m","и"=>"i","т"=>"t",
"ь"=>"\'","б"=>"b","ю"=>"yu",
"Й"=>"I","Ц"=>"C","У"=>"U","К"=>"K","Е"=>"E","Н"=>"N",
"Г"=>"G","Ш"=>"SH","Щ"=>"SH","З"=>"Z","Х"=>"X","Ъ"=>"\'",
"Ф"=>"F","Ы"=>"I","В"=>"V","А"=>"A","П"=>"P","Р"=>"R",
"О"=>"O","Л"=>"L","Д"=>"D","Ж"=>"ZH","Э"=>"IE","Ё"=>"E",
"Я"=>"YA","Ч"=>"CH","С"=>"C","М"=>"M","И"=>"I","Т"=>"T",
"Ь"=>"\'","Б"=>"B","Ю"=>"YU",
);
foreach($replacement as $i=>$u) {
$st = mb_eregi_replace($i,$u,$st);
}
return $st;
}
?>