Simple analysis of PHP keyword replacement class (avoid duplicate substitution, preserve and restore original link) _php instance

Source: Internet
Author: User

PHP keyword Replacement class (avoid duplicate substitution, preserve and restore original link)

The main contents of this section:

A keyword Replacement class

Can be used mainly for keyword filtering, or keyword search substitution.

Implementation Process Analysis:

Keyword replacement, in fact, is a str_replace () process, if it is pure str_replace face 10W keywords, 1W Word article also only need about 2 seconds.

The problem lies in:

Keywords are replaced more than once, such as a need to replace <a>a</a>, but the result may be <a><a>a</a></a> and so on.

To do this, you need a way to protect a label that has already been replaced, so before you process the article, replace it with a label such as [_tnum_] to restore it after the article has been processed.

Another problem, if the keyword or article in the [_tnum_] itself, then you need to eliminate this here can not use the str_replace and need to use the preg_replace to exclude.

Third, if there are two keywords A and ab How to do, hope that the long match off, short after matching, so you need to be sorted before matching.

The last question, when the str_replace changed to Preg_replace later, slowed down the same paragraph 10W match to 5 seconds, string processing functions in the strpos to be faster, then first use Strpos to find the keyword, 10W query less than 1 seconds. Even 1 million is 8 seconds more.

A keyword matching replacement class, code:

code example:

<?php * * keyword matching class * @author ylx <ylx@gmail.com> * @packet Mipang * Use instance * $str = "Green Shell layer" Edwin van der Sar next year, the next year of the spill
  Open room Lucas Defangjun ";
  * $key = new Keyreplace ($str, Array ("XXXX" => "SADF", "Next year" => ' http://baidu.com ', "next year" => ' google.com '));
  * Echo $key->getresulttext ();
  * Echo $key->getruntime ();
 * * Class Keyreplace {private $keys = array ();
 Private $text = "";
 Private $runtime =;
 Private $url = true;
 Private $stopkeys = Array ();
 Private $all = false; /** * @access public * @param string $text Specifies the article to be processed * @param array $keys The specified dictionary phrase array (key=>url,...) URL can be an array, if it is The array will randomly replace one of the * @param array $stopkeys Specify a stop word array (key,...) This word will not be processed * @param boolean $url True to replace the link or replace only * @par Am Boolean $all True indicates that all found words are replaced, otherwise only the first/public function __construct ($text = ', $keys =array (), $url =true, $stopkeys =arr
  Ay (), $all =false) {$this->keys = $keys;
  $this->text = $text;
  $this->url = $url;
  $this->stopkeys = $stopkeys;
 $this->all = $all; }
 /** * Get processed articles * @access public * @return String text */Public Function Getresulttext () {$start = Microti
  Me (true);
  $keys = $this->hits_keys ();
  $keys _tmp = Array_keys () ($keys);
   function cmp ($a, $b) {if (Mb_strlen ($a) = = Mb_strlen ($b)) {return; Return (Mb_strlen ($a) < Mb_strlen ($b))?
  : -;
  } usort ($keys _tmp, "CMP");
   foreach ($keys _tmp as $key) {if (Is_array ($keys [$key])) {$url = $keys [$key][rand (, Count ($keys [$key])];
   }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 word * @access public * @param array $keys Array (key,...) * Public function Setstopkeys ($keys) {$this->stopkeys = $keys; /** * Set Article * @access public * @param string $text/Public Function SetText ($text) {$this->text = $t
 Ext
  /** * Used to find the keyword hit in the string * @access public * @return Array $keys Returns a matching 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 hit in the string * @access public * @return Array $keys returns 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 ();
  /** * Process Substitution * @access private * @param string $text replaced * @param string $key keyword * @param string $url link
  * @return String $text processed articles */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 [];
 foreach ($m [] as $k => $z) {$z = Preg_replace ("#\# #s", "\#", $z); $tmp = preg_replace (' # '. $z. ' #s ', ' [_a '. $k.]
   _] ", $tmp,);
  }
  };
   if (Preg_match_all ("#<[^>]+> #s", $tmp, $m)) {$tags = $m [];
 foreach ($m [] as $k => $z) {$z = Preg_replace ("#\# #s", "\#", $z); $tmp = preg_replace (' # '. $z. ' #s ', ' [_tag '. $k.]
   _] ", $tmp,); } if (!empty ($stop _keys)) {if Preg_match_all ("#". Implode ("|", $stop _keys). "
 #s ", $tmp, $m)) {$stopkeys = $m [];
  foreach ($m [] as $k => $z) {$z = Preg_replace ("#\# #s", "\#", $z); $tmp = preg_replace (' # '. $z. ' #s ', ' [_s '. $k."_]", $tmp,);
  }} $key = Preg_replace ("# ([\#\ (\) \[\]\*]) #s", "\\\\$", $key); if ($this->url) $tmp = Preg_replace ("#" (?!) \[_s|\[_a|\[_|\[_t|\[_ta|\[_tag) ". $key." (?! Ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\] #us ", ' <a href= '. $url. '" > '. $key. '
  </a> ', $tmp, $this->all?-:); else $tmp = preg_replace ("#" (?!) \[_s|\[_a|\[_|\[_t|\[_ta|\[_tag) ". $key." (?!
  Ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\]) #us ", $url, $tmp, $this->all?-:); 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; }
}

Above is the class that this article introduces to you to replace the PHP keyword (avoid duplicate substitution, keep and restore original link).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.