如何做这样的事情:
mb_str_ireplace($search,$replace,$subject,"UTF-8");
这个函数在PHP中不存在,但是我需要有不区分大小写和UTF-8兼容的函数。
例如:我想更改$search -> $replace:
GŁOŚNIKI -> speakers;
Głośniki -> speakers;
głośniki -> speakers;
gŁoŚniki -> speakers;编辑:
欺骗的回答
$text = preg_replace('/głośniki/ui', 'speakers', $text);
对以前的案子有好处。但我还有一个问题。
我想将所有以前版本的"głośniki"更改为<speakers>głośniki</speakers>
GŁOŚNIKI -> <speakers>głośniki</speakers>;
Głośniki -> <speakers>głośniki</speakers>;
głośniki -> <speakers>głośniki</speakers>;
gŁoŚniki -> <speakers>głośniki</speakers>;我的意思是这样的:
mb_str_ireplace(
$search,
$openTag.$replace.$closeTag,
$subject,
"UTF-8"
);发布于 2014-06-03 11:44:07
根据您的编辑,您希望deceze的方法可以与动态变量一起使用,而无需声明所有变量。这对于preg_replace_callback来说非常简单
<?php
header('Content-type: text/plain;');
$string = 'I have a string with a gŁoŚniki word. Pretty interesting';
$word = 'Głośniki';
$string = preg_replace_callback('/(' . preg_quote($word, '/') . ')/ui', function($match) {
return '<speakers>' . mb_strtolower($match[1], 'UTF-8') . '</speakers>';
}, $string);
var_dump($string);输出
string(79) "I have a string with a <speakers>głośniki</speakers> word. Pretty interesting"DEMO
请注意,我使用了preg_quote($word, '/'),以确保我们转义任何字符,可能会混乱的正则表达式(例如。( ?,+,.等)
发布于 2014-06-03 09:28:55
$text = preg_replace('/głośniki/ui', 'speakers', $text);u标志用于UTF-8处理,i用于大小写不敏感.
https://stackoverflow.com/questions/24011509
复制相似问题