/*
- * Keyword Matching class
- * @author YLX
- * @packet Mipang
- * Usage Examples
- * $str = "green shell layer with Edwin van der Sar next year, next year 1 of the spill room Lucas neighbourhoods";
- * $key = new Keyreplace ($str, Array ("xxxx" = "SADF", "next year 1" = ' http://baidu.com ', "next year" = ' 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 $all = false;
- /**
- * @access Public
- * @param string $text Specify the article being processed
- * @param array $keys The specified dictionary phrase array (key=>url,...) URL can be an array, if the array will randomly replace one of them
- * @param array $stopkeys Specify the Stop Word array (key,...) the words inside will not be processed
- * @param boolean $url true means replace with link or replace only
- * @param boolean $all True to replace all found words, or replace only the first time
- */
- 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;
- }
/**
- * Get a good working article
- * @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;
- }
- /**
- * Get processing time
- * @access Public
- * @return Float
- */
- Public Function GetRuntime () {
return $this->runtime;
}
/**
- * Set Keywords
- * @access Public
- * @param array $keys Array (Key=>url,...)
- */
- Public Function SetKeys ($keys) {
$this->keys = $keys;
}
- /**
- * Set Stop words
- * @access Public
- * @param array $keys Array (key,...)
- */
- Public Function Setstopkeys ($keys) {
$this->stopkeys = $keys;
}
- /**
- * Settings article
- * @access Public
- * @param string $text
- */
- Public Function SetText ($text) {
$this->text = $text;
}
/**
- * Used to find the keyword hit in the string
- * @access Public
- * @return Array $keys returns the match to the word 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 ();
- }
/**
- * Used to find the stop word of the string inside the hit
- * @access Public
- * @return Array $keys returns the match to the word 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 ();
- }
/**
- * Handling the replacement process
- * @access Private
- * @param string $text replaced by
- * @param string $key Keywords
- * @param string $url link
- * @return String $text well-handled article
- */
- Private Function r_s ($text, $key, $url) {
$tmp = $text;
$stop _keys = $this->hits_stop_keys ();
$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);
- }
- }
- }
- $key 1 = preg_replace ("# ([\#\ (\) \[\]\*]) #s", "\\\\$1", $key);
if ($this->url)
- $tmp = Preg_replace ("# (?! \[_s|\[_a|\[_|\[_t|\[_ta|\[_tag) ". $key 1." (?! Ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\]) #us ",". $key. ", $tmp, $this->all?-1:1);
- Else
- $tmp = Preg_replace ("# (?! \[_s|\[_a|\[_|\[_t|\[_ta|\[_tag) ". $key 1." (?! 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;
- }
- }
Copy Code |