您好,登錄后才能下訂單哦!
<?php
/*
關鍵詞匹配類
$str = "是是是是是范德薩下一年,下一年誰誰誰水水水水的灑落開是是是是軍";
$key = new KeyReplace($str,array("下一年1"=>'http://baidu.com',"下一年"=>'baidu.com'));
echo $key->getResultText();
echo $key->getRuntime();
*/
$str = <<<ST
做開眼角手術需要開刀。開眼角手術材料的韓式技術,根計開眼角手術方案。
ST;
$g=file_get_contents("t");
$kw=unserialize($g);
$nkw=array();
foreach ($kw as $k=>$g){
if(strpos($k,"開眼角")!==false)
$nkw[$k]=str_replace("php?", "g", $g);
}
// $nkw=array("開眼角"=>"http://hurl1","開眼角手術"=>"http://hurl2");
$key = new KeyWordReplace($str,$nkw);
echo $key->getResultText();
/*
url 中的特殊符號 如 ? . {} 需要做轉義處理 ,替換完成之后再替換回來
echo $key->getRuntime();
*/
function cmp2($a, $b){
$len_a=mb_strlen($a,"utf-8");
$len_b=mb_strlen($b,"utf-8");
if ($len_a == $len_b) return 0;
return ($len_a< $len_b) ? 1 : -1;
}
class KeyWordReplace
{
public $keys = array();
public $text = "";
private $runtime = 0;
public $url = true;
public $stopkeys = array();
public $all = false;
/**
@param boolean $all true 表示替換所有找到的詞,否則只替換第一次
*/
public function __construct($text='',$keys=array(),$url=true,$stopkeys=array(),$all=true) {
$this->keys = $keys;
$this->text = $text;
$this->url = $url;
$this->stopkeys = $stopkeys;
$this->all = $all;
}
/**
@return string text
*/
public function getResultText() {
$start = microtime(true);
$keys = $this->hits_keys();
$keys_tmp = array_keys($keys);
usort($keys_tmp,"cmp2");
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;
}
/**
@return float
*/
public function getRuntime() {
return $this->runtime;
}
/**
@param array $keys array(key=>url,...)
*/
public function setKeys($keys) {
$this->keys = $keys;
}
/**
@param array $keys array(key,...)
*/
public function setStopKeys($keys) {
$this->stopkeys = $keys;
}
/**
@param string $text
*/
public function setText($text) {
$this->text = $text;
}
/**
@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();
}
/**
@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();
}
/**
@return string $text 處理好的文章
*/
private function r_s($text,$key,$url){
$tmp_text = $text;
$stop_keys = $this->hits_stop_keys();
$stopkeys = $tags = $a = array();
if(preg_match_all("#<a[^>]+>[^<]*</a[^>]*>#su",$tmp_text,$m)){
$a=$m[0];
foreach($m[0] as $k=>$z){
$z = preg_replace("#\##s","\#",$z);
$tmp_text = preg_replace('#'.$z.'#s',"[_a".$k."_]",$tmp_text,1);
}
};
if(preg_match_all("#<[^>]+>#s",$tmp_text,$m)){
$tags = $m[0];
foreach($m[0] as $k=>$z){
$z = preg_replace("#\##s","\#",$z);
$tmp_text = preg_replace('#'.$z.'#s',"[_tag".$k."_]",$tmp_text,1);
}
}
if(!empty($stop_keys)){
if(preg_match_all("#".implode("|",$stop_keys)."#s",$tmp_text,$m)){
$stopkeys = $m[0];
foreach($m[0] as $k=>$z){
$z = preg_replace("#\##s","\#",$z);
$tmp_text = preg_replace('#'.$z.'#s',"[_s".$k."_]",$tmp_text,1);
}
}
}
$key1 = preg_replace("#([\#\(\)\[\]\*])#s","\\\\$1",$key);
if($this->url)
$tmp_text = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)".$key1."(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us",'<a href="'.$url.'">'.$key.'</a>',$tmp_text,$this->all?-1:1);
else
$tmp_text = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)".$key1."(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us",$url,$tmp_text,$this->all?-1:1);
if(!empty($a)){
foreach($a as $n=>$at){
$tmp_text = str_replace("[_a".$n."_]",$at,$tmp_text);
}
}
if(!empty($tags)){
foreach($tags as $n=>$at){
$tmp_text = str_replace("[_tag".$n."_]",$at,$tmp_text);
}
}
if(!empty($stopkeys)){
foreach($stopkeys as $n=>$at){
$tmp_text = str_replace("[_s".$n."_]",$at,$tmp_text);
}
}
return $tmp_text;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。