关键字 替换类 替换(随机位置) 且指定数量的关键词

array(“Key”=>”this”,”Href”=>”this“,”ReplaceNumber”=>1),    1=>array(“Key”=>”test”,”Href”=>”test“,”ReplaceNumber”=>1));$str = “this is test content!”;$a = new Seokey($KeyArray,$str);$a->KeyOrderBy();$a->Replaces();echo $a->HtmlString;*/class Seokey{    public $KeyArray;  //关键字    public $HtmlString; //文字内容    public $ArrayCount; //关键字的个数    public $Key;    public $Href;    /*        初始化:        $keyArray 关键字 数组        $String   检索字域,文字    */    function Seokey($KeyArray,$String,$Key='Key',$Href='Href'){       $this->KeyArray=$KeyArray;       $this->HtmlString=$String;       $this->ArrayCount=count($KeyArray);       $this->Key=$Key;       $this->Href=$Href;    }    /*        关键字 按长度排序    */    function KeyOrderBy(){        usort($this->KeyArray,'sortcmp');    }    function Replaces(){                    for($i=0;$i<$this->ArrayCount;$i++){            if((integer)$this->KeyArray[$i]['ReplaceNumber'] != 0 ){                str_replace($this->KeyArray[$i][$this->Key],”/*”.md5($this->KeyArray[$i][$this->Key]).”*/”,$this->HtmlString,$num);//$num查询到的数量                if((integer)$this->KeyArray[$i]['ReplaceNumber']>$num) {//当关键词 需要替换的数量 大于 包含的数量时,替换全部                    $this->KeyArray[$i]['ReplaceNumber']=$num;                    $this->HtmlString=str_replace($this->KeyArray[$i][$this->Key],”/*”.md5($this->KeyArray[$i][$this->Key]).”*/”,$this->HtmlString);                    continue;                }                //当关键词 需要替换的数量 不大于 包含的数量时,使用 KeyStrpos($i);方法替换                $ListNumber=array();                $ListNumber=$this->KeyStrpos($i);//$i: 表示第$i个关键词($i从0开始)                $RegArray=array();                if(count($ListNumber)<1) continue;//不存在 关键词                $n=0;                while($n<(integer)$this->KeyArray[$i][“ReplaceNumber”]){                    $g=0;                    $x=rand(0,count($ListNumber)-1);//随机数                    for($xcn=0;$xcn<=$n;$xcn++){                        if($RegArray[$xcn]==$ListNumber[$x]){                            $g=1;                        }                    }                    if($g==0){                        $RegArray[$n]=$ListNumber[$x];                        $n++;                    }                }                for($c=0;$c$RegArray[$jx]){                            $TempArray=$RegArray[$c];                            $RegArray[$c]=$RegArray[$jx];                            $RegArray[$jx]=$TempArray;                        }                    }                }                for($c=0;$cStrposKey($this->KeyArray[$i][$this->Key],$RegArray[$c],$c);// 逐位(索引位) 替换截取到的关键字                }               $this->HtmlString=str_replace(“/&”.md5($this->KeyArray[$i][$this->Key]).”&/”,$this->KeyArray[$i][$this->Key],$this->HtmlString);            }else{               $this->HtmlString=str_replace($this->KeyArray[$i][$this->Key],”/*”.md5($this->KeyArray[$i][$this->Key]).”*/”,$this->HtmlString);            }       }       for($i=0;$i<$this->ArrayCount;$i++){           $this->HtmlString=str_replace(“/*”.md5($this->KeyArray[$i][$this->Key]).”*/”,$this->KeyArray[$i][$this->Href],$this->HtmlString);       }    }    function StrposKey($Key,$StrNumber,$n){//在字符串里 截取关键字 并替换,从$StrNumber这个位置开始(包含$StrNumber这个位置)替换到$n(包含$n这个位置)这个位置       $this->HtmlString=substr_replace($this->HtmlString, “/*”.md5($Key).”*/”, $StrNumber, 36);    }    /* 递归 查找 关键词 所在的位置 存于数组中 */    function KeyStrpos($KeyId){        $StrListArray=array();        $StrNumberss=strpos($this->HtmlString, $this->KeyArray[$KeyId][$this->Key]);        $xf=0;        while(!($StrNumberss===false)){            $StrListArray[$xf]=$StrNumberss;            $this->HtmlString=substr_replace($this->HtmlString,”/&”.md5($this->KeyArray[$KeyId][$this->Key]).”&/”,$StrNumberss, strlen($this->KeyArray[$KeyId][$this->Key]));            $StrNumberss=strpos($this->HtmlString, $this->KeyArray[$KeyId][$this->Key]);            $xf++;        }        return $StrListArray;    }   }?>

]]>

原创文章,作者:键盘游走者,如若转载,请注明出处:https://www.708034.com/2017/01/%e5%85%b3%e9%94%ae%e5%ad%97-%e6%9b%bf%e6%8d%a2%e7%b1%bb-%e6%9b%bf%e6%8d%a2%e9%9a%8f%e6%9c%ba%e4%bd%8d%e7%bd%ae-%e4%b8%94%e6%8c%87%e5%ae%9a%e6%95%b0%e9%87%8f%e7%9a%84%e5%85%b3%e9%94%ae%e8%af%8d/

发表评论

邮箱地址不会被公开。 必填项已用*标注