/application/common.php
/**
* @todo 敏感词过滤,返回结果
* @param string $string 要过滤的内容
* @return string $log 处理结果
*/
function sensitive($string){
$count = 0; //违规词的个数
$sensitiveWord = ''; //违规词
$list = ['最佳','最具','最爱','最赚最优','最优秀','最好','最大']; //定义敏感词数组,自行添加
$stringAfter = $string; //替换后的内容
$pattern = "/".implode("|",$list)."/i"; //定义正则表达式
if(preg_match_all($pattern, $string, $matches)){ //匹配到了结果
$patternList = $matches[0]; //匹配到的数组
$count = count($patternList);
$sensitiveWord = implode(',', $patternList); //敏感词数组转字符串
$replaceArray = array_combine($patternList,array_fill(0,count($patternList),'*')); //把匹配到的数组进行合并,替换使用
$stringAfter = strtr($string, $replaceArray); //结果替换
}
if($count==0){
return false;
}else{
$log .= "匹配到 [ {$count} ]个敏感词:[ {$sensitiveWord} ]<br/>";
}
return $log;
}
调用
一般放置在update insert前
常用位置 /admin/controller/emptycontroller.php 和 debris.php里
// 敏感词过滤
foreach ( $data as $key => $value ) {
$data_list .= $value ;
}
$res = sensitive($data_list);
if(false !== $res){
$result['msg'] = $res;
$result['code'] = 0;
return $result;
}
效果: