//接收关键词 $str = input('words'); $txt = ComType::field('title,times,id')->select(); //得到所有的过滤词 if (count($txt) > 0) { foreach ($txt as $w) { $k[] = $w['title']; } } //获取数组 foreach ($k as $key => $val) { //去空格 $val = trim($val); //正则过滤 preg_match('/' . $val . '/', $str, $matches); //替换掉敏感词 if (!empty($matches[0])) { $str = str_replace($val, "***", $str); } //收集所有的敏感词 $m[] = $matches; }; //判断是否存在敏感词 $r = strpos($str, '*'); if ($r != false || $r == 0) { //统计匹配次数 if ($m) { foreach ($m as $i) { if ($i) { $t = ComType::where('title', $i[0])->find(); if ($t) { ComType::where('id', $t['id'])->update(['times' => $t['times'] + 1]); } } } } return json(['code' => 1, 'data' => $str, 'msg' => "评论中含有敏感字或词"]); } else { return json(['code' => 2, 'data' => '', 'msg' => "评论中无敏感词或字"]); } }