Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php关键字替换类的实例代码

php关键字替换类的实例代码

发布时间:2017-02-23   编辑:www.jquerycn.cn
分享一例php实现内容关键字替换的类,用于替换(随机位置) 且指定数量的关键词,挺不错的,有需要的朋友参考下吧。

本节主要内容:
php关键字替换类,md5加密后替换,防止重复替换。

1,php关键字替换类
 

复制代码 代码示例:

<?php
/**
*   关键字 替换类  替换(随机位置) 且指定数量的关键词
    先对关键字 md5加密(若关键字之间有重字部分,md5加密,可防止重复替换),替换标志/&--&/
    /*---*/
/*    替换成/*md5*/
/*    最后用 关键词 替换掉 **形式
* by www.jbxue.com
*/
class KeyReplace{
    public $KeyArray;  //关键字
    public $HtmlString; //文字内容
    public $ArrayCount; //关键字 的 个数

    /*
        初始化:
        $keyArray 关键字 数组
        $String   检索字域,文字
    */
    function KeyReplace($KeyArray,$String){
       $this->KeyArray=$KeyArray;
       $this->HtmlString=$String;
       $this->ArrayCount=count($KeyArray);
    }

    /*
        关键字 按长度排序
    */
    function KeyOrderBy(){
        for($i=0;$i<$this->ArrayCount-1;$i++){
            for($j=$i+1;$j<$this->ArrayCount;$j++){
                $TempArray=array();
                if(strlen($this->KeyArray[$i]["Key"])<strlen($this->KeyArray[$j]["Key"])){
                    $TempArray=$this->KeyArray[$i];
                    $this->KeyArray[$i]=$this->KeyArray[$j];
                    $this->KeyArray[$j]=$TempArray;
                }

            }
       }
       // 中国  呵呵 国  中
    }

    function Replaces(){

        for($i=0;$i<$this->ArrayCount;$i++){

            if((integer)$this->KeyArray[$i]['ReplaceNumber'] != 0 ){
                str_replace($this->KeyArray[$i]["Key"],"/*".md5($this->KeyArray[$i]["Key"])."*/",$this->HtmlString,$num);//$num查询到的数量

                if((integer)$this->KeyArray[$i]['ReplaceNumber']>$num) {//当关键词 需要替换的数量 大于 包含的数量时,替换全部
                    $this->KeyArray[$i]['ReplaceNumber']=$num;
                    $this->HtmlString=str_replace($this->KeyArray[$i]["Key"],"/*".md5($this->KeyArray[$i]["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<count($RegArray)-1;$c++)
                {//关键词所在位置 递增排序
                    for($jx=$c+1;$jx<count($RegArray);$jx++){
                        if($RegArray[$c]>$RegArray[$jx]){
                            $TempArray=$RegArray[$c];
                            $RegArray[$c]=$RegArray[$jx];
                            $RegArray[$jx]=$TempArray;
                        }
                    }
                }

                for($c=0;$c<count($RegArray);$c++){
                    $this->StrposKey($this->KeyArray[$i]["Key"],$RegArray[$c],$c);// 逐位(索引位) 替换截取到的关键字
                }

               $this->HtmlString=str_replace("/&".md5($this->KeyArray[$i]["Key"])."&/",$this->KeyArray[$i]["Key"],$this->HtmlString);
            }else{
               $this->HtmlString=str_replace($this->KeyArray[$i]["Key"],"/*".md5($this->KeyArray[$i]["Key"])."*/",$this->HtmlString);
            }
       }

       for($i=0;$i<$this->ArrayCount;$i++){
           $this->HtmlString=str_replace("/*".md5($this->KeyArray[$i]["Key"])."*/",$this->KeyArray[$i]["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]["Key"]);
        $xf=0;
        while(!($StrNumberss===false)){
            $StrListArray[$xf]=$StrNumberss;
            $this->HtmlString=substr_replace($this->HtmlString,"/&".md5($this->KeyArray[$KeyId]["Key"])."&/",$StrNumberss, strlen($this->KeyArray[$KeyId]["Key"]));
            $StrNumberss=strpos($this->HtmlString, $this->KeyArray[$KeyId]["Key"]);
            $xf++;
        }
        return $StrListArray;
    }
}
?>

2,php关键字替换类的调用示例
 

复制代码 代码示例:

<?php
$KeyArray1=Array(
    0=>array("Key"=>"中国","Href"=>"<a href='中国'>中国</a>","ReplaceNumber"=>1),
    1=>array("Key"=>"中","Href"=>"<a href='中'>中</a>","ReplaceNumber"=>1),
    2=>array("Key"=>"国","Href"=>"<a href='国'>国</a>","ReplaceNumber"=>1),
    3=>array("Key"=>"呵呵","Href"=>"<a href='呵呵'>呵呵</a>","ReplaceNumber"=>0)
);
$KeyArray=Array(
    0=>array("Key"=>"中国","Href"=>"<a href='中国'>中国</a>","ReplaceNumber"=>1),//只替换一个‘中国’
    1=>array("Key"=>"人","Href"=>"<a href='人'>人</a>","ReplaceNumber"=>1),
    2=>array("Key"=>"我","Href"=>"<a href='我'>我</a>","ReplaceNumber"=>1),
    3=>array("Key"=>"呵呵","Href"=>"<a href='呵呵'>呵呵</a>","ReplaceNumber"=>0)//全部替换
);
$str = "呵呵!中国人民站起来了,中国,我的祖国,呵呵!中国是我们的,中国也是你们的,中国人民严厉谴责他们,我们中国人就是OK12呵呵!";
$a = new KeyReplace($KeyArray,$str);
$a->KeyOrderBy();
$a->Replaces();

//header("content-type: text/html; charset=utf-8");//设置页面编码
echo $a->HtmlString;
?>

您可能感兴趣的文章:
php替换html内容的小函数
PHP字符串替换函数 可同时替换多个关键词
PHP实现文章内容添加内链关键词替换的代码
PHP文章内链实例代码 php关键词替换且只替换一次
php 字符串替换函数学习
mysql中upate-替换字段内部分内容的例子
php自动给文章加关键词链接的实现代码
PHP替换字符串(只替换首个字符串)
php中static,const与define的区别分析
PHP替换数组键名的小例子

[关闭]