PHP——敏感词过滤

前言

如果可以用第三方的话,那么你是幸运的,因为现在这种敏感词过滤,敏感图片,敏感语音过滤的第三方服务还是挺多的

 

敏感词过滤

核心代码

利用PHP内置的三个函数

array_combine() | array_fill() | strtr()

$replace =array_combine($item,array_fill(0,count($item),'*'));
$content = strtr($content,$replace);

array_combine

 

 array_fill

 

 strtr

 

 

完整代码

//过滤敏感词所有匹配的敏感词用一个*代替
$sensitives = Db::name('sensitive')->field('data')->cache(7200)->select();
$item = [];
foreach($sensitives as $k=>$v){
 $item[$k] = $v['data'];
}
$replace =array_combine($item,array_fill(0,count($item),'*'));
$content = strtr($content,$replace);

 

posted @ 2019-04-02 16:46  。思索  阅读(243)  评论(0编辑  收藏  举报