php过滤敏感词
2024-09-03 23:56:32
<?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;
}
}
最新文章
- 网页变灰的CSS代码
- android 兼容性测试 CTS 测试过程(实践测试验证通过)
- 中文版Windows 7下设置日语格式布局的键盘
- mldn android
- UR #13 Ernd
- 浅谈MySQL 数据库性能优化
- apache-maven-3.2.1设备
- iOS 登陆之界面设置
- [笔记]A*寻路算法初探
- java IO之 序列流 集合对象Properties 打印流 流对象
- D&#160;区间求和 [数学 树状数组]
- 痞子衡嵌入式:开启NXP-MCUBootUtility工具的HAB加密功能 - CST(中英双语)
- Spring教程笔记(2) IOC
- EntityFramework Core并发深挖详解,一纸长文,你准备好看完了吗?
- zabbix系列~mysql进行监控
- javascript OOP实例—探测器
- appDesign
- ABP框架系列之二十九:(Hangfire-Integration-延迟集成)
- HTML的简介
- 1067 - Combinations---LightOj(Lucas求组合数)
热门文章
- kafka安装linux版
- 配置git diff和git merge使用的第三方工具
- java 字符串转json,json转实体对象、json字符串转换成List、List转String、以及List排序等等...
- 玩转 SpringBoot 2 快速整合 Filter
- mysql5.7新增加用户和授权
- -Gradle 翻译 Merge AndroidManifest 合并清单文件 MD
- .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
- spark-sql使用笔记
- writeAsBytes writeAsString
- 突然用xcode老版本调试老代码测试新机,报错";Could not find Developer Disk Image";回忆下