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

常见的两种敏感词过滤的方式
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;
    }
}

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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值