-
违规字符数组
$patternArr = [‘123’,’你好’,’你’,’好’,’aaa’];
-
匹配字符串
$str = “你好123aaa的JFK大家”;
-
匹配结果
替换结果 bbbbbbbbb的JFK大家 匹配结果 Array ( [0] => Array ( [0] => 你好 [1] => 123 [2] => aaa ) )
-
完整代码
$patternArr = ['123','你好','你','好','aaa']; $str = "你好123aaa的JFK大家"; //组装正则表达式 $pattern = "/"; foreach($patternArr as $item){ $pattern .="$item|"; } $pattern = rtrim($pattern,'|')."/"; //匹配结果 $matchs = []; preg_match_all($pattern,$str,$matchs); $str = preg_replace($pattern,"bbb",$str); echo "替换结果\n".$str; echo "\n匹配结果\n"; print_r($matchs);
php正则匹配所有违规字符
最新推荐文章于 2022-06-14 19:37:53 发布