php关键词命中
/**
* @describe 数组生成正则表达式
* @param array $words
* @return string
*/
function generateRegularExpression($words)
{
$regular = implode('|', array_map('preg_quote', $words));
return "/$regular/i";
}
/**
* @describe 字符串 生成正则表达式
* @param array $words
* @return string
*/
function generateRegularExpressionString($string){
$str_arr[0]=$string;
$str_new_arr= array_map('preg_quote', $str_arr);
return $str_new_arr[0];
}
/**
* 检查敏感词
* @param $banned
* @param $string
* @return array
*/
function check_words($words,$string)
{ $match_banned=array();
//循环查出所有敏感词
$banned=generateRegularExpression($words);
$new_banned=strtolower($banned);
$i=0;
do{
$matches=[];
if (!empty($new_banned) && preg_match($new_banned, $string, $matches)) {
$isempyt=empty($matches[0]);
if(!$isempyt){
$match_banned = array_merge($match_banned, $matches);
$matches_str=strtolower(generateRegularExpressionString($matches[0]));
$new_banned=str_replace("|".$matches_str."|","|",$new_banned);
$new_banned=str_replace("/".$matches_str."|","/",$new_banned);
$new_banned=str_replace("|".$matches_str."/","/",$new_banned);
}
}
$i++;
if($i>20){
$isempyt=true;
break;
}
}while(count($matches)>0 && !$isempyt);
//查出敏感词
if($match_banned){
return $match_banned;
}
//没有查出敏感词
return array();
}
$words=['呵呵1','哈哈2','哟哟3'];
var_dump(check_words($words,'呵呵1唉哟哟哟3哟阿斯顿发撒地方了阿斯顿发卡上的饭馆'));

被以下专题收入,发现更多相似内容
© 2025 iuin.cc 版权所有 冀ICP备2023033630号-1 网站邮箱:mrzhaozn@gmail.com