<?php
/**
 * 敏感词过滤工具类
 * 使用方法
 * echo FilterTools::filterContent("你妈的我操一色狼杂种二山食物","*",DIR."config/word.txt",$GLOBALS["p_memcache"]["bad_words"]);
 */
 
class FilterTools {
    public static $keyword = array();
    /**
     * 从文件中加载敏感词
     * @param $filename
     * @return array
     */
    static function getBadWords($filename){
        $file_handle = fopen($filename, "r");
        while (!feof($file_handle)) {
            $line = trim(fgets($file_handle));
            array_push(self::$keyword,$line);
        }
        fclose($file_handle);
        return self::$keyword;
    }
 
    /**
     * @param $content 待处理字符串
     * @param $target  替换后的字符
     * @param $filename  敏感词配置文件
     * @param $memconfig 缓存配置文件
     * @return 处理后的字符串
     */
    static function filterContent($content,$target,$filename,$memconfig){
        $mem = new BadWordsMemcache($filename,$memconfig);
        $keyword = $mem->getList();
        if(count($keyword) == 0){
            $keyword = self::getBadWords($filename);
        }
        return strtr($content, array_combine( $keyword, array_fill(0,count($keyword), $target)));
    }
 
}
 
/**
 * 敏感词缓存处理类
 * Class BadWordsMemcache
 */
class BadWordsMemcache{
    var $memcache;
    var $key;
    var $list;
    var $filename;
    function __construct($filename,$memconfig) {
        $this->filename = $filename;
        if(!class_exists("P_Memcache")){
            require_once DIR."lib/memcache.class.php";
        }
        $this->key = "bad_words";
        $this->memcache = new P_Memcache();
        $this->memcache->config = $memconfig;
        $this->memcache->connect();
        print_r($this->memcache);
        $this->init();
    }
    function __destruct() {
        $this->memcache->close();
    }
 
    /**
     * 初始化
     * @param bool $isReset
     */
    function init($isReset = false){
        $this->list = $this->memcache->get($this->key)?$this->memcache->get($this->key):array();
        if(count($this->list)==0 || $isReset){
            $this->list = filterTools::getBadWords($this->filename);
            $this->memcache->set($this->key, $this->list);
            $log_data = Log::formatData($this->list);
            Log::logWrite($log_data, 'bad.words','init');
        }
    }
 
    /**
     * 获取列表
     * @return mixed
     */
    function getList(){
        return $this->list;
    }
}

php过滤敏感词的更多相关文章

  1. web前端js过滤敏感词

    web前端js过滤敏感词 这里是用文本输入框还有文本域绑定了失去焦点事件,然后再遍历敏感词数组进行匹配和替换. var keywords=["阿扁","呵呵", ...

  2. (转)两种高效过滤敏感词算法--DFA算法和AC自动机算法

    原文:https://blog.csdn.net/u013421629/article/details/83178970 一道bat面试题:快速替换10亿条标题中的5万个敏感词,有哪些解决思路? 有十 ...

  3. 【SpringBoot】前缀树 Trie 过滤敏感词

    1.过滤敏感词 Spring Boot实践,开发社区核心功能 完成过滤敏感词 Trie 名称:Trie也叫做字典树.前缀树(Prefix Tree).单词查找树 特点:查找效率高,消耗内存大 应用:字 ...

  4. SpringBoot开发十四-过滤敏感词

    项目需求-过滤敏感词 利用 Tire 树实现过滤敏感词 定义前缀树,根据敏感词初始化前缀树,编写过滤敏感词的方法 代码实现 我们首先把敏感词存到一个文件 sensitive.txt: 赌博 嫖娼 吸毒 ...

  5. 过滤敏感词工具类SensitiveFilter

    网上过滤敏感词工具类有的存在挺多bug,这是我自己改用的过滤敏感词工具类,目前来说没啥bug,如果有bug欢迎在评论指出 使用前缀树 Trie 实现的过滤敏感词,树节点用静态内部类表示了,都写在一个 ...

  6. [转]Filter实现处理中文乱码,转义html标签,过滤敏感词

    原文地址:http://www.cnblogs.com/xdp-gacl/p/3952405.html 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可 ...

  7. js 过滤敏感词 ,可将带有标点符号的敏感词过滤掉

    function transSensitive(content) { // var Sensitive = H.getStorage("Sensitive");//敏感词数组 va ...

  8. PHP 扩展 trie-tree, swoole过滤敏感词方案

    在一些app,web中评论以及一些文章会看到一些*等,除了特定的不显示外,我们会把用户输入的一些敏感字符做处理,具体显示为*还是其他字符按照业务区实现. 下面简单介绍下业务处理. 原文地址:小时刻个人 ...

  9. js 过滤敏感词

    <html> <head>     <title>Bad Words Example</title>     <script type=" ...

随机推荐

  1. Spring Boot 项目的 API 接口防刷

    首先是写一个注解类 拦截器中实现 注册到springboot中 在Controller中加入注解 说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点:springbo ...

  2. 解决netty客户端接收报文不完整的情况

    逻辑就是在处理handler前加入一个处理符,然后 channelReadComplete这个事件进行处理.同时注意客服端的配置: public void connect(String addr, i ...

  3. c++11多线程记录1 -- std::thread

    启动一个线程 话不多说,直接上代码 void func(); int main() { std::thread t(func); //这里就开始启动线程了 t.join(); // 必须调用join或 ...

  4. Tr/ee AtCoder - 4433 (构造)

    大意: 给定长$n$的字符串$s$, 要求构造一棵树, 满足若第$i$个字符为$1$, 那么可以删一条边, 得到一个大小为$i$的连通块. 若为$0$则表示不存在一条边删去后得到大小为$i$的连通块. ...

  5. Netty源码分析之NioEventLoop(三)—NioEventLoop的执行

    前面两篇文章Netty源码分析之NioEventLoop(一)—NioEventLoop的创建与Netty源码分析之NioEventLoop(二)—NioEventLoop的启动中我们对NioEven ...

  6. Spring-Cloud之开篇

    一.为什么会有spring-cloud.随着现代互联网的发展,以前很多传统的单体项目将不再满足于现在的互联网需求,而这个时候就诞生了另外一种说法,微服务.简单理解就是将软件应用程序独立部署的服务的一中 ...

  7. 1.ASP.NET Core 中向 Razor Pages 应用添加模型

    右键单击“RazorPagesMovie”项目 >“添加” > “新建文件夹”. 将文件夹命名为“Models”.右键单击“Models”文件夹. 选择“添加” > “类”. 将类命 ...

  8. Java—十进制数对n进制数转换

    import java.math.BigInteger;import java.util.Scanner; /** * @auther Aohui * @create 2019-11-06-15:33 ...

  9. Python的virtualenv管理

    原文链接 虚拟环境 Python 开发中所谓的虚拟环境,就是为 Python 版本及第三方库创建独立的开发环境,使不同项目之间互不干扰.借助于虚拟环境,我们可以在同一台电脑上构建出项目 A 在基于 P ...

  10. 把项目中的vant UI组件升级

    首先把之前 的VANT 卸载掉 npm uninstall vant 然后重新安装 一次vant npm i vant -S