- <?php
- class fillter{
- private $keyword_file;
- private $dict;
- public $result;
- public function __construct($file){
- if(!is_file($file)){
- trigger_error("$file not exists!");
- }
- $this->keyword_file=$file;
- }
- public function fill($resource){
- $this->dict =
$this->getDict();
- $len = strlen($resource);
- for($i=0; $i<$len; ++$i){
- $key=substr($resource,$i,2);
- if(array_key_exists($key,$this->dict)){
-
$this->deal(substr($resource,$i,$this->dict[$key]['max']),$key,$af);
- $i+=$af;
- }
- else{
- $this->result .=substr($resource,$i,1);
- }
- }
- return $this->result;
- }
- public function deal($res,$key,&$af){
- $af=0;
- foreach($this->dict[$key]['list'] as
$keyword){
- if(strpos($res,$keyword) !==false){
- $len=strlen($keyword);
- $af=$len-1;
- $this->result .=str_repeat("*",$len);
- return;
- }
- }
- $this->result .= substr($res,0,1);
- }
- private function getKeyWordList(){
- $keywords =
file_get_contents($this->keyword_file);
- return array_unique(explode("\r\n",$keywords));
- }
- public function getDict(){
- $keywords=$this->getKeyWordList();
- $dict=array();
- foreach($keywords as $keyword){
- if(empty($keyword)){
- continue;
- }
- $key = substr($keyword,0,2);
- $dict[$key]['list'][]=$keyword;
-
$dict[$key]['max']=max($dict[$key]['max'],strlen($keyword));
- }
- return $dict;
- }
- }
- //测试用demo
结果比直接用正则提高了3倍效率
- $res = $text = file_get_contents("test.txt");
- $t1 = microtime(true);
- $fil = new fillter("banwords.txt");
- $res = $fil->fill($text);
- //$res = $fil->getDict();
- //$keywords =
explode("\r\n",file_get_contents("banwords.txt"));
- //foreach($keywords as $keyword){
- // if(empty($keyword)){
- // continue;
- // }
- // $res = preg_replace("/$keyword/","***",$res);
- //}
- $t2 = microtime(true);
- echo $t2-$t1;
- echo $text,"<br/>";
- echo "<pre>";
- print_r( $res);
- echo "</pre>";
- ?>
青春就应该这样绽放 游戏测试:三国时期谁是你最好的兄弟!! 你不得不信的星座秘密
posted @
2012-06-07 00:25
goodlad
阅读(
1078)
评论()
收藏
举报