php过滤敏感词
<?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; } }
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks agoByDDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks agoByDDD
Where to find the Crane Control Keycard in Atomfall
3 weeks agoByDDD
Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation
1 months agoByDDD
Roblox: Dead Rails - How To Complete Every Challenge
3 weeks agoByDDD

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools