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=" ...
随机推荐
- AutoIt实现软件自动化安装
AutoIt下载安装 1.下载:https://www.autoitscript.com/site/autoit/downloads/ 2.安装,一直点下一步 3.安装好可以看到开始菜单如下(需要用到 ...
- MyBatis参数条件查询传入的值为0时的判断
MyBatis条件查询对字段判断是否为空一般为: <if test="testValue!=null and testValue != ''"> and test_va ...
- 洛谷P2048 [NOI2010]超级钢琴 题解
2019/11/14 更新日志: 近期发现这篇题解有点烂,更新一下,删繁就简,详细重点.代码多加了注释.就酱紫啦! 正解步骤 我们需要先算美妙度的前缀和,并初始化RMQ. 循环 \(i\) 从 \(1 ...
- 利用MySQL存储过程批量插入100W条测试数据
DROP PROCEDURE IF EXISTS insert_batch; CREATE PROCEDURE insert_batch() BEGIN ; loopname:LOOP '); ; T ...
- Java的访问修饰符的作用范围
访问修饰符: private default protected public 作用范围: 访问修饰符\作用范围 所在类 同一包内其他类 其他包内子类 其他包内非子类 private 可以访问 不可以 ...
- Stack布局中定位的方式
//……省略无关代码…… child: new Column( children: <Widget>[ new SizedBox(height: 20.0), new Stack( ali ...
- spring boot 规范json返回值
spring boot 规范json返回值 spring boot 接口返回配置 @ResponseBody ,则返回自定义的对象,解析成json. 但是,部分字段能友好的展示出来.如 Date,Lo ...
- 【转载】C#中double.TryParse方法和double.Parse方法的异同之处
在C#编程过程中,double.TryParse方法和double.Parse方法都可以将字符串string转换为double类型,但两者还是有区别,最重要的区别在于double.TryParse方法 ...
- hbase完整分布式集群搭建
简介: hadoop的单机,伪分布式,分布式安装 hadoop2.8 集群 1 (伪分布式搭建 hadoop2.8 ha 集群搭建 hbase完整分布式集群搭建 hadoop完整集群遇到问题汇总 Hb ...
- Oracle UNDO块
过程:开始一个事务--通过事务信息找到UNDO块头的所在的段名及数据文件号等--转储UNDO header--在事务表中对应槽位找到前镜像dba--转储数据块--找到对应记录得到bdba--转储数据块 ...