Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mb_ereg_replace not replacing while preg_replace works as intended

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);

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 is same as of that if $name in the above case. What am I doing wrong?

like image 809
Jithin Avatar asked Aug 29 '10 12:08

Jithin


1 Answers

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);
like image 152
Artefacto Avatar answered Sep 18 '22 13:09

Artefacto