敏感词过滤

 

敏感词=***
过滤=***
...

 <?php

/**
 *   文本中的非法字符过滤类 
 */
class Filter{
	private $pattern;
	private $replacement;
	/**
	 * 构造函数,进行初始化工作,加载替换规则,并构造用于模式查找和模式替换的两个数组
	 */
	public function __construct(){
		$f= fopen("filter.txt","r");
		if(!$f){
			die("open filter file failed!");
		}
		while (!feof($f))
		{
			$line = fgets($f);//从过滤规则文件中读取一行记录
			$patternandreplace = explode('=', $line); //用等号分割,前面的用作模式串,后面的用作替换串
			$this->pattern[] = '/'.$patternandreplace[0].'/';
			$this->replacement[] = trim($patternandreplace[1]);
		}
		fclose($f);
		
	}
	/**
	 * 去除$source中的敏感字符,用*替换
	 * @param unknown_type $source
	 */
	public function clean($source){
		return preg_replace($this->pattern,$this->replacement,$source);
	}
};

你可能感兴趣的:(过滤)