Php keyword replacement class (avoid repeated replacement, retain and restore the original link)

Source: Internet
Author: User
Php keyword replacement class (avoid repeated replacement, retain and restore the original link)

  1. /*

  2. * Keyword Matching class
  3. * @ Author ylx
  4. * @ Packet mipang
  5. * Use instances
  6. * $ Str = "the green shell egg zafdensa's local army in the room will be scattered and scattered in the next year and in the next year ";
  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 specifies the article to be processed
  22. * @ Param array $ keys specifies the dictionary phrase array (key => url,...) url can be an array. if it is an array, a random replacement
  23. * @ Param array $ stopkeys specify the stop word array (key,...). the words in this field will not be processed.
  24. * @ Param boolean $ url true: replace the value with a link. otherwise, replace the value with only one link.
  25. * @ Param boolean $ all true indicates that all words found are replaced; otherwise, only the first time is replaced.
  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 the processed articles
  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. * Obtain the 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 the stop word.
  78. * @ Access public
  79. * @ Param array $ keys array (key ,...)
  80. */
  81. Public function setStopKeys ($ keys ){

  82. $ This-> stopkeys = $ keys;

  83. }

  84. /**
  85. * Set an 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 hit keywords in the string
  94. * @ Access public
  95. * @ Return array $ keys returns the matched 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 hit in the string
  114. * @ Access public
  115. * @ Return array $ keys returns the matched 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
  136. * @ Param string $ key keyword
  137. * @ Param string $ url link
  138. * @ Return string $ text-processed articles
  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. $ Key1 = preg_replace ("# ([# \ (\) \ [\] \ *]) # s", "\\\\ $1", $ key );

  168. If ($ this-> url)

  169. $ Tmp = preg_replace ("#(?! \ [_ S | \ [_ a | \ [_ | \ [_ t | \ [_ ta | \ [_ tag) ". $ key1 ."(?! Ag \ d + _ \] | g \ d + _ \] | s \ d + _ \] | _ \]) # us ", ''. $ key. '', $ tmp, $ this-> all? -1:1 );
  170. Else
  171. $ Tmp = preg_replace ("#(?! \ [_ S | \ [_ a | \ [_ | \ [_ t | \ [_ ta | \ [_ tag) ". $ key1 ."(?! Ag \ d + _ \] | g \ d + _ \] | s \ d + _ \] | _ \]) # us ", $ url, $ tmp, $ this-> all? -1:1 );

  172. If (! Empty ($ )){

  173. Foreach ($ a as $ n => $ ){

  174. $ Tmp = str_replace ("[_ a". $ n. "_]", $ at, $ tmp );

  175. }

  176. }

  177. If (! Empty ($ tags )){

  178. Foreach ($ tags as $ n =>$ ){

  179. $ Tmp = str_replace ("[_ tag". $ n. "_]", $ at, $ tmp );

  180. }

  181. }

  182. If (! Empty ($ stopkeys )){

  183. Foreach ($ stopkeys as $ n => $ ){

  184. $ Tmp = str_replace ("[_ s". $ n. "_]", $ at, $ tmp );

  185. }

  186. }

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

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.