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){
$DDDCD="";
$DDn=0;
$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);
$DDDCD="敏感内容已被过滤。";
$DDn=$DDn+1;
$DDw="[";
$DDw.=$res;
$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 $DDDCD;
if( $DDn>1){
echo"<SCRIPT STYLE=\"TEXT/JAVASCRIPT\">";
echo"alert('有以下";
echo $DDn;
echo"个字符被作为关键字过滤:";
echo $DDw;
echo ",如果您以为这是个错误,请与我们联系')</SCRIPT>";
}
=======================================
这是一个通过屏蔽字库,希望提示有多少\哪些字衬衫被屏蔽的提示方法
现在问题是试测时说$DDDCD;$DDn未定义

解决方案 »

  1.   

    $DDDCD 你应该定义为 public static $DDDCD 下面访问的时候 self::$DDDCD 访问。因为$DDDCD已经被定义为全局变量了。在类中访问时需要用$this->$DDDCD 进行访问。
      

  2.   

    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=$DDn+1;
    $this->DDw="[";
    $this->DDw.=$res;
    $this->DDw.="]";return;}echo $fil->DDDCD;
    if( $fil->DDn>1){
    echo"<SCRIPT STYLE=\"TEXT/JAVASCRIPT\">";
    echo"alert('有以下";
    echo $fil->DDn;
    echo"个字符被作为关键字过滤:";
    echo $fil->DDw;
      

  3.   

    ====
    有这样的提示
    Strict Standards: Accessing static property fillter::$DDDCD as non static in D:\web\slyb\indexbs218.php on line 61
     
    Strict Standards: Accessing static property fillter::$DDn as non static in D:\web\slyb\indexbs218.php on line 62
     
    Strict Standards: Accessing static property fillter::$DDw as non static in D:\web\slyb\indexbs218.php on line 63
     
    Strict Standards: Accessing static property fillter::$DDDCD as non static in D:\web\slyb\indexbs218.php on line 70
     
    Notice: Undefined variable: DDn in D:\web\slyb\indexbs218.php on line 71
     
    Strict Standards: Accessing static property fillter::$DDn as non static in D:\web\slyb\indexbs218.php on line 71
     
    Strict Standards: Accessing static property fillter::$DDw as non static in D:\web\slyb\indexbs218.php on line 72
     
    Strict Standards: Accessing static property fillter::$DDw as non static in D:\web\slyb\indexbs218.php on line 73
     
    ======
    class fillter{
    private $keyword_file;
    private $dict;
    public $result;
    public static $DDDCD;
    public static  $DDn;
    public static $DDw;
    我这样设置是否有误??
      

  4.   

    不是 public 类型的吗?怎么又改成 public static 啦?
    类型不同,访问的方式也不同!!!!
      

  5.   

    =====
    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;
    }
    }
    //测试用demo 结果比直接用正则提高了3倍效率
    //$res = $text = file_get_contents("text.txt");
    $res= $text =$getmaospc1;
    $t1 = microtime(true);
    $fil = new fillter("signify_list.txt");
    $res= $fil->fill($text);
    $getmaospc=$res;
    $dddfr= new fillter();
    echo $fil->DDDCD;
    if( $fil->DDn>1){
    echo"<SCRIPT STYLE=\"TEXT/JAVASCRIPT\">";
    echo"alert('有以下";
    echo $fil->DDn;
    echo"个字符被作为关键字过滤:";
    echo $fil->DDw;echo ",如果您以为这是个错误,请与我们联系')</SCRIPT>";
    }
    ===========
    我改了回来
    请斑斑看一下
    还是有以下提示
    Warning: Missing argument 1 for fillter::__construct(), called in D:\web\slyb\indexbs218.php on line 112 and defined in D:\web\slyb\indexbs218.php on line 33
     
    Notice: Undefined variable: file in D:\web\slyb\indexbs218.php on line 35
     
    Notice: Undefined variable: file in D:\web\slyb\indexbs218.php on line 36
     
    Notice: not exists! in D:\web\slyb\indexbs218.php on line 36
     
    Notice: Undefined variable: file in D:\web\slyb\indexbs218.php on line 38
     
      

  6.   

    小伙纸,代码在下。主要是class成员变量在内部用this访问,外部用类变量名访问<?php
    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("ssssssssssssssssssssssssssssssssss");
    $res= $fil->fill($text);
    $getmaospc=$res;echo $fil->DDDCD;
    if( $fil->DDn>1){
    echo"<SCRIPT STYLE=\"TEXT/JAVASCRIPT\">";
    echo"alert('有以下";
    echo $fil->DDn;
    echo"个字符被作为关键字过滤:";
    echo $fil->DDw;
    echo ",如果您以为这是个错误,请与我们联系')</SCRIPT>";
    }