我正在使用正则表达式从给定字符串中获取多个模式。 在这里,我将向您解释清楚。
$string = "about us";
$newtag = preg_replace("/ /", "_", $string);
print_r($newtag);
以上是我的代码。
在这里,我正在查找单词中的空格并将空格替换为我需要的特殊字符,对吧??
现在,我需要一个正则表达式来给我这样的模式
about_us
、about-us
、aboutus
作为输出,如果我将 about us
作为输入。
这样可以吗。
请帮助我。
提前致谢!
最佳答案
最后,我的答案是
$string = "contact_us";
$a = array('-','_',' ');
foreach($a as $b){
if(strpos($string,$b)){
$separators = array('-','_','',' ');
$outputs = array();
foreach ($separators as $sep) {
$outputs[] = preg_replace("/".$b."/", $sep, $string);
}
print_r($outputs);
}
}
exit;
关于php - 用于从给定字符串中查找多个模式的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20682907/