PHP Sensitive Word filtering

Source: Internet
Author: User
  1. /**
  2. * Forbidden Word Filter
  3. * Execution efficiency: 0.05 seconds per piece
  4. * @author Liuxu
  5. *
  6. */
  7. Class Logic_blackword
  8. {
  9. Const App_forum = 1;
  10. Const APP_BLOG = 2;
  11. Const APP_VOTE = 3;
  12. /**
  13. * Filter to get forbidden words
  14. * @param unknown $txt
  15. * @return ambigous
  16. */
  17. Public Function gethitlist ($txt)
  18. {
  19. $hitList = Array ();
  20. Filtering Forbidden words in batches
  21. $max = $this->getmax ();
  22. if ($max)
  23. {
  24. $size = 1000;
  25. $last = Ceil ($max/$size);
  26. for ($page =1; $page <= $last; $page + +)
  27. {
  28. $result = $this->gethitlistbypage ($txt, $page, $size);
  29. if ($result) $hitList = Array_merge ($hitList, $result);
  30. }
  31. }
  32. $hitList 2 = array ();
  33. foreach ($hitList as $hit = $type)
  34. {
  35. $hitList 2[$type] [] = $hit;
  36. }
  37. return $hitList 2;
  38. }
  39. Private Function Getmax ()
  40. {
  41. $redis = Rds::factory ();
  42. $memKey = ' Blackword_max ';
  43. $max = $redis->get ($memKey);
  44. if ($max ===false)
  45. {
  46. $max = 0;
  47. $blackWord = new Model_blackword_blackword ();
  48. $para [' field '] = "max (ID) as Max";
  49. $result = $blackWord->search ($para);
  50. if (Isset ($result [0][' Max ')) $max = $result [0][' Max '];
  51. $redis->setex ($memKey, $max);
  52. }
  53. return $max;
  54. }
  55. /**
  56. * Batch filter to get banned words
  57. * @param unknown $txt
  58. * @param number $page
  59. * @param number $size
  60. * @return Multitype:ambigous
  61. */
  62. Private Function Gethitlistbypage ($txt, $page =1, $size =1000)
  63. {
  64. $hitList = Array ();
  65. To get the forbidden word tree in batches
  66. $wordTree = $this->getwordtreebypage ($page, $size);
  67. $txt = Strip_tags ($txt);
  68. $txt = preg_replace ('/[^a-za-z0-9\\x{4e00}-\\x{9fa5}]/iu ', ' ', $txt);
  69. $len = Mb_strlen ($txt, ' UTF-8 ');
  70. for ($i =0; $i < $len; $i + +)
  71. {
  72. $char = Mb_substr ($txt, $i, 1, ' UTF-8 ');
  73. if (Isset ($wordTree [$char]))
  74. {
  75. $result = $this->gethitlistbytree (mb_substr ($txt, $i, A, ' UTF-8 '), $wordTree);
  76. if ($result)
  77. {
  78. foreach ($result as $hit = $type)
  79. {
  80. $hitList [$hit] = $type;
  81. }
  82. }
  83. }
  84. }
  85. return $hitList;
  86. }
  87. /**
  88. * Whether to ban words
  89. * @param str $txt
  90. * @param arr $wordTree
  91. * @return Multitype:unknown
  92. */
  93. Private Function Gethitlistbytree ($txt,& $wordTree)
  94. {
  95. $len = Mb_strlen ($txt, ' UTF-8 ');
  96. $point = & $wordTree;
  97. $hit = ";
  98. $hitList = Array ();
  99. for ($i =0; $i < $len; $i + +)
  100. {
  101. $char = Mb_substr ($txt, $i, 1, ' UTF-8 ');
  102. if (Isset ($point [$char]))
  103. {
  104. $hit. = $char;
  105. $point = & $point [$char];
  106. if (Isset ($point [' type ']))//Match succeeded
  107. {
  108. $hitList [$hit] = $point [' type '];
  109. }
  110. }
  111. Else
  112. {
  113. Break
  114. }
  115. }
  116. return $hitList;
  117. }
  118. /**
  119. * Get banned word trees in batches
  120. * @param int $page
  121. * @param int $size
  122. * @return Arr:
  123. */
  124. Private Function Getwordtreebypage ($page =1, $size =1000)
  125. {
  126. $redis = Rds::factory ();
  127. $memKey = ' blackword_tree_ '. $page. ' _ '. $size;
  128. $wordTree = $redis->get ($memKey);
  129. if ($wordTree ===false)
  130. {
  131. $wordTree = Array ();
  132. $blackWord = new Model_blackword_blackword ();
  133. $start = ($page-1) * $size;
  134. $end = $start + $size;
  135. $para [' where '] = "Status=1 and id>". $start. " and id<= ". $end;
  136. $result = $blackWord->search ($para);
  137. if ($result)
  138. {
  139. foreach ($result as $value)
  140. {
  141. if ($value [' word '])
  142. {
  143. $value [' word '] = preg_split ('/(? $point = & $wordTree;
  144. foreach ($value [' word '] as $char)
  145. {
  146. $point = & $point [$char];
  147. }
  148. $point [' type '] = $value [' type '];
  149. }
  150. }
  151. }
  152. $redis->setex ($memKey, $wordTree);
  153. }
  154. return $wordTree;
  155. }
  156. }
Copy Code
Php
  • 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.