-
-
/*
- * 關鍵字匹配類別
- * @author ylx * @author ylx
- * @packet mipang
- * 使用實例
- * $str = "綠殼蛋雞撒范德薩下一年,下一年1的灑落開房間盧卡斯地方軍";
- * $key = new KeyReplace($str,array("xxxx"=>"sadf","下一年1"=>'http://baidu.com',"下一年"=>'google. com'));
- * echo $key->getResultText();
- * echo $key->getRuntime();
- */
- class KeyReplace
- {
- private $ keys = array();
- private $text = "";
- private $runtime = 0;
- private $url = true;
- private $stopkeys = array();
- private $private $ all = false;
- /**
- * @access public
- * @param string $text 指定被處理的文章
- * @param array $keys 指定字典詞組array(key=>url,...) url可以是數組,如果是數組將隨機替換其中的一個
- * @param array $stopkeys 指定停止詞array(key,...) 這裡面的詞將不會被處理
- * @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) }
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;
- }
- /**
- * 取得處理時間
- * @access public
- * @return float
- */
- public function getRuntime() {
-
return $this->runtime;
}
/**
- * 設定關鍵字
- * @access public
- * @param array $keys array(key=>url,...)
- */
- public function setKeys($keys) {
$this-> keys = $keys;
}
- /**
- * 設定停止字
- * @access public
- * @param array $keys array(key,...)
- */
- public function setStopKeys($keys) {
$this ->stopkeys = $keys;
}
- /**
- * 設定文章
- * @access public
- * @param string $text
- */
- public function setText($text) {
$this->text = $text;
}
/**
- * 用來找出字串裡面命中的關鍵字
- * @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 hit_stop_keys(){
- $ar = $this->stopkeys;
- $ar = $ar?$ar :array();
- $ result=array();
- $str = $this->text;
- foreach($ar as $k){
- $k = trim($k) ;
- if(!$ k)
- 繼續;
- if(strpos($str,$k)!==false && in_array($k,$this->stopkeys)){
- $結果[] = $k;
- }
- }
- 回傳 $result?$result:array();
- }
/**
- * 處理替換過程
- * @access private
- * @param string $text 被替換者
- * @param string $key 關鍵字
- * @param string $url 連結
- * @return string $text 處理好的文章
- */
- 蕨函數 r_s ($text,$key,$url){
$tmp = $text;
$stop_keys = $this->hits_stop_keys(); p>
$stopkeys = $tags = $a = array();
- if(preg_match_all("#]+>[^] *> ;#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."(?!agd +_]|gd+_]|d+_] | sd+_]|_])#us",''.$key.'',$tmp,$this->all?-1 : 1);
- else
- $tmp = preg_replace("#(?![_s|[_a|[_|[_t|] [_ta|[_tag)]".$key1."(?! agd+ _]|gd+_]|d+_]|sd+_]|_])#us",$url,$tmp,$this ->全部?-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);
}
}
- 回傳$tmp;
- }
- }
複製程式碼
|