php过滤敏感词
<?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过滤敏感词的更多相关文章
- web前端js过滤敏感词
web前端js过滤敏感词 这里是用文本输入框还有文本域绑定了失去焦点事件,然后再遍历敏感词数组进行匹配和替换. var keywords=["阿扁","呵呵", ...
- (转)两种高效过滤敏感词算法--DFA算法和AC自动机算法
原文:https://blog.csdn.net/u013421629/article/details/83178970 一道bat面试题:快速替换10亿条标题中的5万个敏感词,有哪些解决思路? 有十 ...
- 【SpringBoot】前缀树 Trie 过滤敏感词
1.过滤敏感词 Spring Boot实践,开发社区核心功能 完成过滤敏感词 Trie 名称:Trie也叫做字典树.前缀树(Prefix Tree).单词查找树 特点:查找效率高,消耗内存大 应用:字 ...
- SpringBoot开发十四-过滤敏感词
项目需求-过滤敏感词 利用 Tire 树实现过滤敏感词 定义前缀树,根据敏感词初始化前缀树,编写过滤敏感词的方法 代码实现 我们首先把敏感词存到一个文件 sensitive.txt: 赌博 嫖娼 吸毒 ...
- 过滤敏感词工具类SensitiveFilter
网上过滤敏感词工具类有的存在挺多bug,这是我自己改用的过滤敏感词工具类,目前来说没啥bug,如果有bug欢迎在评论指出 使用前缀树 Trie 实现的过滤敏感词,树节点用静态内部类表示了,都写在一个 ...
- [转]Filter实现处理中文乱码,转义html标签,过滤敏感词
原文地址:http://www.cnblogs.com/xdp-gacl/p/3952405.html 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可 ...
- js 过滤敏感词 ,可将带有标点符号的敏感词过滤掉
function transSensitive(content) { // var Sensitive = H.getStorage("Sensitive");//敏感词数组 va ...
- PHP 扩展 trie-tree, swoole过滤敏感词方案
在一些app,web中评论以及一些文章会看到一些*等,除了特定的不显示外,我们会把用户输入的一些敏感字符做处理,具体显示为*还是其他字符按照业务区实现. 下面简单介绍下业务处理. 原文地址:小时刻个人 ...
- js 过滤敏感词
<html> <head> <title>Bad Words Example</title> <script type=" ...
随机推荐
- 【记录】【java】JDK8新特性Stream方式遍历集合
由于是以流方式,所以怎么操作也不改变原来的集合 1.普通遍历forEach List<String> list = new ArrayList(); list.add("a&qu ...
- python 必选参数、默认参数、可变参数和、关键字参数
转自:https://www.liaoxuefeng.com/wiki/897692888725344/897693568201440 可变参数 在Python函数中,还可以定义可变参数.顾名思义,可 ...
- tp中model加载机制
$user_model = D('User'); 如果当前模块下面有UserModel,就优先使用当前模块下的UserModel.如果当前模块下没有UserModel,就回去Common模块下找Use ...
- 【转帖】Infor转型十年启示录:ERP套件厂商为什么要做云平台?
Infor转型十年启示录:ERP套件厂商为什么要做云平台? https://www.tmtpost.com/4199274.html 好像浪潮国际 就是用的infor的ERP软件. 秦聪慧• 2019 ...
- Node.js 开发指南-读书笔记
1. Node.js 使用了单 线程.非阻塞的事件编程模式 Node.js 最大的特点就是采用异步式 I/O 与事件驱动的架构设计.对于高并发的解决方 案,传统的架构是多线程模型,也就是为每个业务逻辑 ...
- 长乐国庆集训Day5
T1 方阵 题目 [题目描述] 小澳最近迷上了考古,他发现秦始皇的兵马俑布局十分有特点,热爱钻研的小澳打算在电脑上还原这个伟大的布局. 他努力钻研,发现秦始皇布置兵马俑是有一定规律的.兵马俑阵总共有n ...
- Python基础(七)——文件和异常
1.1 读取整个文件 我们可以创建一个 test.txt 并写入一些内容,使用 Python 读文件操作,读出文本内容. with open(r'E:\test.txt') as file_objec ...
- CLRS10.2-7练习 - 翻转单向列表
要求: Give a Θ(n)-time nonrecursive procedure that reverses a singly linked list of nelements. The pro ...
- MNIST机器学习入门(二)
在前一个博客中,我们已经对MNIST 数据集和TensorFlow 中MNIST 数据集的载入有了基本的了解.本节将真正以TensorFlow 为工具,写一个手写体数字识别程序,使用的机器学习方法是S ...
- ajax标准写法
ajax 标准写法 $.ajax({ url:"http://www.microsoft.com", //请求的url地址 dataType:"json", / ...