PHP mb_ereg_replace 未替换,而 preg_replace 按预期工作

发布于 2024-09-16 09:47:21 字数 463 浏览 3 评论 0原文

我正在尝试将字符串中所有非单词字符替换为空字符串(除了空格),并将所有多个空格放在一起作为一个空格。

以下代码执行此操作。

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-09-23 09:47:21

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

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);
她如夕阳 2024-09-23 09:47:21
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

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; 

}

please check is it ok and support english and unicode

执手闯天涯 2024-09-23 09:47:21

输入不是 多字节,因此是 mb 函数失败。

The input is not Multi-Byte hence the mb function fails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文