当preg_replace按预期工作时,PHP mb_ereg_replace不会被替换

时间:2010-08-29 12:03:17

标签: php regex pcre multibyte

我试图用字符串替换所有非单词字符,空字符串期望空格,并将所有多个空格放在一起作为一个单独的空格。

以下代码执行此操作。

$cleanedString = preg_replace('/[^\w]/', ' ', $name);  
$cleanedString = preg_replace('/\s+/', ' ', $cleanedString);

但是当我尝试使用mb_ereg_replace时,没有任何反应。

$cleanedString = mb_ereg_replace('/[^\w]/', ' ', $name);  
$cleanedString = mb_ereg_replace('/\s+/', ' ', $cleanedString);

$ cleaningString与上述情况下的$ name相同。我做错了什么?

3 个答案:

答案 0 :(得分:10)

mb_ereg_replace不使用分隔符。您可能也可能不必在之前指定编码。

mb_regex_encoding("UTF-8");
//regex could also be \W
$cleanedString = mb_ereg_replace('[^\w]', ' ', $name);
$cleanedString = mb_ereg_replace('\s+', ' ', $cleanedString);

答案 1 :(得分:-1)

function create_slug_html($string, $ext='.html'){     
   $replace = '-';         
   $string=strtolower($string);     
   $string=trim($string);

    mb_regex_encoding("UTF-8");
    //regex could also be \W
    $string= mb_ereg_replace('[^\w]', ' ', $string);
    $string= mb_ereg_replace('\s+', ' ', $string);

   //remove query string     
   if(preg_match("#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i",$string)){         
         $parsed_url = parse_url($string);         
         $string = $parsed_url['host'].' '.$parsed_url['path'];         
         //if want to add scheme eg. http, https than uncomment next line         
         //$string = $parsed_url['scheme'].' '.$string;     
   }      
   //replace / and . with white space     
   $string = preg_replace("/[\/\.]/", " ", $string);   

   // $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);  

   //remove multiple dashes or whitespaces     
   $string = preg_replace("/[\s-]+/", " ", $string);   

   //convert whitespaces and underscore to $replace     
   $string = preg_replace("/[\s_]/", $replace, $string);     
   //limit the slug size     
   $string = substr($string, 0, 200);     
   //slug is generated     
   return ($ext) ? $string.$ext : $string; 

}

请检查它是否正常并支持英语和unicode

答案 2 :(得分:-3)

输入不是Multi-Byte,因此mb函数失败。