Reprint: http://www.169it.com/blog_article/601549531.html
The main contents of this section:
A class that replaces a keyword
Can be used mainly for keyword filtering, or keyword search and replacement aspects.
Implementation Process Analysis:
Keyword replacement, in fact, is a str_replace () process, if the simple str_replace face 10W keywords, 1W words of the article also only need about 2 seconds.
The problem lies in:
The keyword replaced more than once, such as a need to replace a, but the result may be a and so on.
To do this, you need a way to protect the labels that have already been replaced, so before you work on the article, replace the label, such as [_tnum_] after the article has been processed and then restore it.
Another question, if the keyword or article has [_tnum_] itself what to do, then need to exclude this here can not use str_replace and need to use the preg_replace with regular to exclude.
The third question, if there are two keywords A and ab What to do, I would like to first long match out, short after matching, so you need to sort before matching.
The last problem, when Str_replace changed to Preg_replace after, slowed the same paragraph 10W times match to 5 seconds, string processing function Strpos to faster, then first use Strpos to find the key words, 10W queries less than 1 seconds. Even 1 million is 8 seconds more.
A keyword match replaces the class, code:
code example:
/*
* 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;
}
}
The above describes the PHP keyword replacement class to avoid duplication, to retain and restore the original link, including the content of the area, I hope that the PHP tutorial interested in a friend helpful.