php
/**
* Created by JetBrains PhpStorm.
* User: lsl
* Date: 13-8-28
* Time: 下午2:58
* 敏感词过滤工具类
* 使用方法
* 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
* User: lsl
*/
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;
}
}
0
0
相关文章
php获取本机ip用fsockopen可行吗_php实现方法及注意【步骤】
php数组怎么筛除null和空串_php数组null空串剔除【步骤】
php代码示例如何发送邮件_php发送邮件代码示例【示例】
php如何判断变量为实现某接口_php接口实现检测方式【技巧】
php代码示例如何生成短链接_php生成短链接代码示例【示例】
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
2026赚钱平台入口大全
2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。
76
2026.01.31
无需付费的漫画app大全
想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。
67
2026.01.31
漫画免费在线观看地址大全
想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。
19
2026.01.31
热门下载
相关下载
精品课程
相关推荐
/
热门推荐
/
最新课程
最新文章







