您好,登錄后才能下訂單哦!
<?php
/*
echo $key->getRuntime();
*/
class KeyReplace
{
private $keys = array();
private $text = "";
private $runtime = 0;
private $url = true;
private $stopkeys = array();
private $all = false;
/**
/**
@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;
}
/**
@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 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",'<a href="'.$url.'">'.$key.'</a>',$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;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。