違禁詞庫以及使用方法
鏈接:違禁詞庫下載
提取碼:ife1
tp6爲例
1.先創建一個違禁詞庫的php文件,我這邊是放在extend文件夾下面了
然後封裝個公共方法
use contraband\word;
/**
* [敏感字過濾]
* @param $content 需要驗證的字符串
* @param $verify 是否提示驗證
* @return 替換後的內容
*/
function lexVerify($content = '', $verify = false)
{
if(!$content) return false;
$wordObject = new word(); // 引入敏感字詞庫
$word = $wordObject->word();
$lexicon = array_combine($word,array_fill(0,count($word),'*')); // 換字符
$str = strtr($content, $lexicon); // 匹配替換
if($verify) {
if($str != $content) {
$res = array('status'=>0,'msg'=>'文字存在敏感詞');
}else{
$res = array('status'=>1,'msg'=>'驗證成功');
}
}else{
$res = array('status'=>1,'msg'=>'替換成功','text'=>$str);
}
return $res;
}
如此簡單完事,關鍵是對array_fill(),array_combine() strtr()函數的瞭解