您好,登錄后才能下訂單哦!
<?php /* * 關(guān)鍵詞匹配類 * @author ylx <ylx@gmail.com> * @packet mipang * 使用實(shí)例 * $str = "綠殼蛋雞撒范德薩下一年adasa,下一年的灑落開房間盧卡斯地方軍"; * $key = new KeyReplace($str,array("xxxx"=>"<a ss>sadf</a ss>","下一年"=>'<a ss>http://baidu.com</a>',"下一年"=>'<a ss>google.com<a ss>')); * echo $key->getResultText(); * echo $key->getRuntime(); */ $str = "綠殼蛋雞撒范德薩下一年adasa,下一年的灑<a href>下一年</a>落開房間盧卡斯地方軍"; $k=new KeyWordReplace($str,array("薩下一年adasa"=>'<a href="google.com">下一年3<a ss>',"下一年"=>'<a href="google.com">下一年2</a>')); echo $k->getResultText(); /* * 關(guān)鍵詞匹配類 * $str = "是是是是是范德薩下一年,下一年誰誰誰水水水水的灑落開是是是是軍"; * $key = new KeyReplace($str,array("下一年1"=>'http://baidu.com',"下一年"=>'baidu.com')); * echo $key->getResultText(); * echo $key->getRuntime(); */ class KeyWordReplace { private $keys = array(); private $text = ""; private $runtime = 0; private $url = true; private $stopkeys = array(); private $all = false; /** * @access public * @param string $text 指定被處理的文章 * @param array $keys 指定字典詞組array(key=>url,...) url可以是數(shù)組,如果是數(shù)組將隨機(jī)替換其中的一個(gè) * @param array $stopkeys 指定停止詞array(key,...) 這里面的詞將不會(huì)被處理 * @param boolean $url true 表示替換成鏈接否則只替換 * @param boolean $all true 表示替換所有找到的詞,否則只替換第一次 */ public function __construct($text='',$keys=array(),$url=true,$stopkeys=array(),$all=false) { $this->keys = $keys; $this->text = $text; $this->url = $url; $this->stopkeys = $stopkeys; $this->all = $all; } /** * 獲取處理好的文章 * @access public * @return string text */ public function getResultText() { $start = microtime(true); $keys = $this->hits_keys(); $keys_tmp = array_keys($keys); function cmp($a, $b){ if (mb_strlen($a) == mb_strlen($b)) { return 0; } return (mb_strlen($a) < mb_strlen($b)) ? 1 : -1; } usort($keys_tmp,"cmp"); foreach($keys_tmp as $key){ if(is_array($keys[$key])){ $url = $keys[$key][rand(0,count($keys[$key])-1)]; }else $url = $keys[$key]; $this->text = $this->r_s($this->text,$key,$url); } $this->runtime = microtime(true)-$start; return $this->text; } /** * 獲取處理時(shí)間 * @access public * @return float */ public function getRuntime() { return $this->runtime; } /** * 設(shè)置關(guān)鍵詞 * @access public * @param array $keys array(key=>url,...) */ public function setKeys($keys) { $this->keys = $keys; } /** * 設(shè)置停止詞 * @access public * @param array $keys array(key,...) */ public function setStopKeys($keys) { $this->stopkeys = $keys; } /** * 設(shè)置文章 * @access public * @param string $text */ public function setText($text) { $this->text = $text; } /** * 用來找到字符串里面命中的關(guān)鍵詞 * @access public * @return array $keys 返回匹配到的詞array(key=>url,...) */ public function hits_keys(){ $ar = $this->keys; $ar = $ar?$ar:array(); $result=array(); $str = $this->text; foreach($ar as $k=>$url){ $k = trim($k); if(!$k) continue; if(strpos($str,$k)!==false && !in_array($k,$this->stopkeys)){ $result[$k] = $url; } } return $result?$result:array(); } /** * 用來找到字符串里面命中的停止詞 * @access public * @return array $keys 返回匹配到的詞array(key,...) */ public function hits_stop_keys(){ $ar = $this->stopkeys; $ar = $ar?$ar:array(); $result=array(); $str = $this->text; foreach($ar as $k){ $k = trim($k); if(!$k) continue; if(strpos($str,$k)!==false && in_array($k,$this->stopkeys)){ $result[] = $k; } } return $result?$result:array(); } /** * 處理替換過程 * @access private * @param string $text 被替換者 * @param string $key 關(guān)鍵詞 * @param string $url 鏈接 * @return string $text 處理好的文章 */ private function r_s($text,$key,$url){ $tmp = $text; $stop_keys = $this->hits_stop_keys(); $stopkeys = $tags = $a = array(); if(preg_match_all("#<a[^>]+>[^<]*</a[^>]*>#su",$tmp,$m)){ $a=$m[0]; foreach($m[0] as $k=>$z){ $z = preg_replace("#\##s","\#",$z); $tmp = preg_replace('#'.$z.'#s',"[_a".$k."_]",$tmp,1); } }; if(preg_match_all("#<[^>]+>#s",$tmp,$m)){ $tags = $m[0]; foreach($m[0] as $k=>$z){ $z = preg_replace("#\##s","\#",$z); $tmp = preg_replace('#'.$z.'#s',"[_tag".$k."_]",$tmp,1); } } if(!empty($stop_keys)){ if(preg_match_all("#".implode("|",$stop_keys)."#s",$tmp,$m)){ $stopkeys = $m[0]; foreach($m[0] as $k=>$z){ $z = preg_replace("#\##s","\#",$z); $tmp = preg_replace('#'.$z.'#s',"[_s".$k."_]",$tmp,1); } } } $key1 = preg_replace("#([\#\(\)\[\]\*])#s","\\\\$1",$key); if($this->url) $tmp = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)".$key1."(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us",$url,$tmp,$this->all?-1:1); else $tmp = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)".$key1."(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us",$url,$tmp,$this->all?-1:1); if(!empty($a)){ foreach($a as $n=>$at){ $tmp = str_replace("[_a".$n."_]",$at,$tmp); } } if(!empty($tags)){ foreach($tags as $n=>$at){ $tmp = str_replace("[_tag".$n."_]",$at,$tmp); } } if(!empty($stopkeys)){ foreach($stopkeys as $n=>$at){ $tmp = str_replace("[_s".$n."_]",$at,$tmp); } } return $tmp; } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。