class fillter{
private $keyword_file;
private $dict;
public $result;
public $DDDCD;
public $DDn;
public $DDw; 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;
}
/*
**匹配到了关键字时的处理
**$res 源字符串
**$keywords 关键字数组
*/
public function deal($res,$key,&$af){
$this->DDDCD="";
$this->DDn=0;
$this->DDw="";
$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);
$this->DDDCD="敏感内容已被过滤。";
$this->DDn=$this->DDn+1;
$this->DDw="[";
$this->DDw.=$res;
$this->DDw.="]"; 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;
}
}$res= $text =$getmaospc1;
$t1 = microtime(true);
$fil = new fillter("signify_list.txt");
$res= $fil->fill($text);
$getmaospc=$res;echo $fil->DDDCD;
if( $fil->DDn>0){
echo"<script LANGUAGE='javascript'>";
echo"alert('有以下";
echo $fil->DDn;
echo"个字符被作为关键字过滤:";
echo $fil->DDw;
echo ",如果您以为这是个错误,请与我们联系')</script>";
}
==============
希望对红字部分的DDn DDW进行累计 但好象只能得到最后的值
private $keyword_file;
private $dict;
public $result;
public $DDDCD;
public $DDn;
public $DDw; 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;
}
/*
**匹配到了关键字时的处理
**$res 源字符串
**$keywords 关键字数组
*/
public function deal($res,$key,&$af){
$this->DDDCD="";
$this->DDn=0;
$this->DDw="";
$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);
$this->DDDCD="敏感内容已被过滤。";
$this->DDn=$this->DDn+1;
$this->DDw="[";
$this->DDw.=$res;
$this->DDw.="]"; 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;
}
}$res= $text =$getmaospc1;
$t1 = microtime(true);
$fil = new fillter("signify_list.txt");
$res= $fil->fill($text);
$getmaospc=$res;echo $fil->DDDCD;
if( $fil->DDn>0){
echo"<script LANGUAGE='javascript'>";
echo"alert('有以下";
echo $fil->DDn;
echo"个字符被作为关键字过滤:";
echo $fil->DDw;
echo ",如果您以为这是个错误,请与我们联系')</script>";
}
==============
希望对红字部分的DDn DDW进行累计 但好象只能得到最后的值
$this->DDw.=$res;
$this->DDw.="]";
你的本意可能不需要return
$this->DDDCD = "";
$this->DDn = 0;
$this->DDw = "";
$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);
$this->DDDCD = "敏感内容已被过滤。";
$this->DDn = $this->DDn + 1;
$this->DDw = "[";
$this->DDw .= substr($res, 0, 1);
$this->DDw .= "]";
}
}
//$this->result .= substr($res, 0, 1); //不在循环中,没效果
return $this->result;
}//或者返回二维数组
function deal($res, $key, & $af) {
$this->DDDCD = "";
$this->DDn = 0;
$this->DDw = "";
$af = 0;
$arr = array();
foreach ($this->dict[$key]['list'] as $keyword) {
if (strpos($res, $keyword) !== false) {
$len = strlen($keyword);
$af = $len -1;
$this->result .= str_repeat("*", $len);
$this->DDDCD = "敏感内容已被过滤。";
$this->DDn = $this->DDn + 1;
$this->DDw = "[";
$this->DDw .= substr($res, 0, 1);
$this->DDw .= "]";
$arr['DDn'][] = $this->DDn;
$arr['DDw'][] = $this->DDw;
}
}
return $arr;
}
这里必须RETURN
否则就出错了,
这是一个根据signify_list.txt内容,对提交的某段文字进行逐个对比的方法