PHP Conference Japan 2024

mb_eregi_replace

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

mb_eregi_replace忽略大小写并支持多字节的正则表达式替换

描述

mb_eregi_replace(
    字符串 $pattern,
    字符串 $replacement,
    字符串 $string,
    ?字符串 $options = null
): 字符串|false|null

扫描 string 中与 pattern 匹配的内容,然后将匹配的文本替换为 replacement

参数

pattern

正则表达式模式。可以使用多字节字符。将忽略大小写。

replacement

替换文本。

string

要搜索的 字符串

options
搜索选项。有关说明,请参见 mb_regex_set_options()

返回值

生成的 字符串 或错误时的 false。如果 string 对当前编码无效,则返回 null

变更日志

版本 描述
8.0.0 options 现在可以为空。
7.1.0 该函数检查 string 是否对当前编码有效。
7.1.0 e 修饰符已被弃用。

注释

注意:

内部编码或由 mb_regex_encoding() 指定的字符编码将用作此函数的字符编码。

警告

在处理不受信任的输入时,切勿使用 e 修饰符。不会发生自动转义(如 preg_replace() 中所知)。如果不注意这一点,很可能会在您的应用程序中创建远程代码执行漏洞。

参见

添加注释

用户贡献的注释 2 条注释

kirilatpillaxdotcom
12 年前
突出显示多字节文本中的单词

<?php
$s
= 'Алабала';
$f = 'а';
echo
preg_replace('/('.$f.')/iu', '<b>$1</b>', $s);
?>
cat at feelthehouse dot ru
16 年前
用于 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;
}
?>
To Top