PHP keyword substitution classes (avoid duplicate substitutions, keep and restore original links)

Source: Internet
Author: User
  1. /*

  2. * Keyword Matching class
  3. * @author YLX
  4. * @packet Mipang
  5. * Usage Examples
  6. * $str = "green shell layer with Edwin van der Sar next year, next year 1 of the spill room Lucas neighbourhoods";
  7. * $key = new Keyreplace ($str, Array ("xxxx" = "SADF", "next year 1" = ' http://baidu.com ', "next year" = ' google.com '));
  8. * Echo $key->getresulttext ();
  9. * Echo $key->getruntime ();
  10. */
  11. Class Keyreplace
  12. {
  13. Private $keys = Array ();
  14. Private $text = "";
  15. Private $runtime = 0;
  16. Private $url = true;
  17. Private $stopkeys = Array ();
  18. Private $all = false;
  19. /**
  20. * @access Public
  21. * @param string $text Specify the article being processed
  22. * @param array $keys The specified dictionary phrase array (key=>url,...) URL can be an array, if the array will randomly replace one of them
  23. * @param array $stopkeys Specify the Stop Word array (key,...) the words inside will not be processed
  24. * @param boolean $url true means replace with link or replace only
  25. * @param boolean $all True to replace all found words, or replace only the first time
  26. */
  27. Public function __construct ($text = ", $keys =array (), $url =true, $stopkeys =array (), $all =false) {
  28. $this->keys = $keys;
  29. $this->text = $text;
  30. $this->url = $url;
  31. $this->stopkeys = $stopkeys;
  32. $this->all = $all;
  33. }

  34. /**

  35. * Get a good working article
  36. * @access Public
  37. * @return String text
  38. */
  39. Public Function Getresulttext () {
  40. $start = Microtime (true);
  41. $keys = $this->hits_keys ();

  42. $keys _tmp = Array_keys ($keys);

  43. function cmp ($a, $b) {

  44. if (Mb_strlen ($a) = = Mb_strlen ($b)) {
  45. return 0;
  46. }
  47. Return (Mb_strlen ($a) < Mb_strlen ($b))? 1:-1;
  48. }

  49. Usort ($keys _tmp, "CMP");

  50. foreach ($keys _tmp as $key) {

  51. if (Is_array ($keys [$key])) {

  52. $url = $keys [$key][rand (0,count ($keys [$key])-1)];
  53. }else
  54. $url = $keys [$key];

  55. $this->text = $this->r_s ($this->text, $key, $url);

  56. }

  57. $this->runtime = Microtime (True)-$start;

  58. return $this->text;

  59. }
  60. /**
  61. * Get processing time
  62. * @access Public
  63. * @return Float
  64. */
  65. Public Function GetRuntime () {

  66. return $this->runtime;

  67. }

  68. /**

  69. * Set Keywords
  70. * @access Public
  71. * @param array $keys Array (Key=>url,...)
  72. */
  73. Public Function SetKeys ($keys) {

  74. $this->keys = $keys;

  75. }

  76. /**
  77. * Set Stop words
  78. * @access Public
  79. * @param array $keys Array (key,...)
  80. */
  81. Public Function Setstopkeys ($keys) {

  82. $this->stopkeys = $keys;

  83. }

  84. /**
  85. * Settings article
  86. * @access Public
  87. * @param string $text
  88. */
  89. Public Function SetText ($text) {

  90. $this->text = $text;

  91. }

  92. /**

  93. * Used to find the keyword hit in the string
  94. * @access Public
  95. * @return Array $keys returns the match to the word array (Key=>url,...)
  96. */
  97. Public Function Hits_keys () {
  98. $ar = $this->keys;
  99. $ar = $ar? $ar: Array ();
  100. $result =array ();
  101. $str = $this->text;
  102. foreach ($ar as $k = = $url) {
  103. $k = Trim ($k);
  104. if (! $k)
  105. Continue
  106. if (Strpos ($str, $k)!==false &&!in_array ($k, $this->stopkeys)) {
  107. $result [$k] = $url;
  108. }
  109. }
  110. Return $result? $result: Array ();
  111. }

  112. /**

  113. * Used to find the stop word of the string inside the hit
  114. * @access Public
  115. * @return Array $keys returns the match to the word array (key,...)
  116. */
  117. Public Function Hits_stop_keys () {
  118. $ar = $this->stopkeys;
  119. $ar = $ar? $ar: Array ();
  120. $result =array ();
  121. $str = $this->text;
  122. foreach ($ar as $k) {
  123. $k = Trim ($k);
  124. if (! $k)
  125. Continue
  126. if (Strpos ($str, $k)!==false && in_array ($k, $this->stopkeys)) {
  127. $result [] = $k;
  128. }
  129. }
  130. Return $result? $result: Array ();
  131. }

  132. /**

  133. * Handling the replacement process
  134. * @access Private
  135. * @param string $text replaced by
  136. * @param string $key Keywords
  137. * @param string $url link
  138. * @return String $text well-handled article
  139. */
  140. Private Function r_s ($text, $key, $url) {

  141. $tmp = $text;

  142. $stop _keys = $this->hits_stop_keys ();

  143. $stopkeys = $tags = $a = Array ();

  144. if (Preg_match_all ("#]+>[^<]*]*> #su", $tmp, $m)) {
  145. $a = $m [0];

  146. foreach ($m [0] as $k = + $z) {

  147. $z = Preg_replace ("#\# #s", "\#", $z);

  148. $tmp = preg_replace (' # '. $z. ' #s ', "[_a". $k. " _] ", $tmp, 1);

  149. }

  150. };

  151. if (Preg_match_all ("#<[^>]+> #s", $tmp, $m)) {

  152. $tags = $m [0];
  153. foreach ($m [0] as $k = + $z) {
  154. $z = Preg_replace ("#\# #s", "\#", $z);
  155. $tmp = preg_replace (' # '. $z. ' #s ', "[_tag". $k. " _] ", $tmp, 1);
  156. }
  157. }
  158. if (!empty ($stop _keys)) {
  159. if (Preg_match_all ("#". Implode ("|", $stop _keys). " #s ", $tmp, $m)) {
  160. $stopkeys = $m [0];
  161. foreach ($m [0] as $k = + $z) {
  162. $z = Preg_replace ("#\# #s", "\#", $z);
  163. $tmp = preg_replace (' # '. $z. ' #s ', "[_s". $k. " _] ", $tmp, 1);
  164. }
  165. }
  166. }
  167. $key 1 = preg_replace ("# ([\#\ (\) \[\]\*]) #s", "\\\\$1", $key);

  168. if ($this->url)

  169. $tmp = Preg_replace ("# (?! \[_s|\[_a|\[_|\[_t|\[_ta|\[_tag) ". $key 1." (?! Ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\]) #us ",". $key. ", $tmp, $this->all?-1:1);
  170. Else
  171. $tmp = Preg_replace ("# (?! \[_s|\[_a|\[_|\[_t|\[_ta|\[_tag) ". $key 1." (?! Ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\]) #us ", $url, $tmp, $this->all?-1:1);

  172. if (!empty ($a)) {

  173. foreach ($a as $n = = $at) {

  174. $tmp = Str_replace ("[_a". $n. " _] ", $at, $tmp);

  175. }

  176. }

  177. if (!empty ($tags)) {

  178. foreach ($tags as $n = = $at) {

  179. $tmp = Str_replace ("[_tag". $n. " _] ", $at, $tmp);

  180. }

  181. }

  182. if (!empty ($stopkeys)) {

  183. foreach ($stopkeys as $n = = $at) {

  184. $tmp = Str_replace ("[_s". $n. " _] ", $at, $tmp);

  185. }

  186. }

  187. return $tmp;
  188. }
  189. }

Copy Code
  • 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.