当preg_replace按预期工作时,PHP mb_ereg_replace无法替换 [英] PHP mb_ereg_replace not replacing while preg_replace works as intended
本文介绍了当preg_replace按预期工作时,PHP mb_ereg_replace无法替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将字符串中所有非单词字符替换为空字符串(期望空格),然后将所有多个空格放在一起作为一个空格.
I am trying to replace in a string all non word characters with empty string expect for spaces and the put together all multiple spaces as one single space.
下面的代码可以做到这一点.
Following code does this.
$cleanedString = preg_replace('/[^\w]/', ' ', $name);
$cleanedString = preg_replace('/\s+/', ' ', $cleanedString);
但是当我尝试使用mb_ereg_replace时,什么也没发生.
But when I am trying to use mb_ereg_replace nothing happens.
$cleanedString = mb_ereg_replace('/[^\w]/', ' ', $name);
$cleanedString = mb_ereg_replace('/\s+/', ' ', $cleanedString);
$ cleanedString与上述情况下的$ name相同.我在做什么错了?
$cleanedString is same as of that if $name in the above case. What am I doing wrong?
推荐答案
mb_ereg_replace
不使用分隔符.您可能必须也可能不必之前指定编码.
mb_ereg_replace
doesn't use separators. You may or may not also have to specify the encoding before.
mb_regex_encoding("UTF-8");
//regex could also be \W
$cleanedString = mb_ereg_replace('[^\w]', ' ', $name);
$cleanedString = mb_ereg_replace('\s+', ' ', $cleanedString);
这篇关于当preg_replace按预期工作时,PHP mb_ereg_replace无法替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文