<?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{