敏感词过滤——你中招了吗?

常见的两种敏感词过滤的方式
1、第三方接口过滤(小程序自带接口、第三方阿里、聚合数据);
2、敏感词库过滤(本文讲解这个)
老规矩直接上代码
注:词库下载地址(永久有效):
链接:https://pan.baidu.com/s/1OTZIqUKPgT-P3lCaedewYQ
提取码:8888

<?php
namespace  app\api\controller;
header("Content-Type: text/html; charset=utf-8");
class Fiter{
    //修改关键词 过滤
    public function get_words($content="中华共和国"){
        //是否为空
        if(empty($content)){
            return $content;
        }
        //是否为 字符串
        if(!is_string($content)){
            strtr($content);
        }
        $file_path = $_SERVER['DOCUMENT_ROOT'].'/application/api/controller/minganciku/mgck2017/key.txt';//这里填写自己词库路径
        $words = '';
        if(file_exists($file_path)) {
            $words = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
        }
        if($words){
            $badword = explode('|',$words);
            $badword1 = array_combine($badword,array_fill(0,count($badword),str_repeat('*',3)));
            $str = strtr($content,$badword1);
            return $str;
        }
        return $content;
    }
}

直接在你需要过滤的地方调用即可

猜你喜欢

转载自blog.csdn.net/weixin_39860188/article/details/107917202
今日推荐