PHP sensitive word filtering

Source: Internet
Author: User
PHP sensitive word filtering

  1. /**
  2. * Banned word filtering
  3. * Execution efficiency: each article takes 0.05 seconds.
  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 out banned words
  14. * @ Param unknown $ txt
  15. * @ Return Ambigous
  16. */
  17. Public function getHitList ($ txt)
  18. {
  19. $ HitList = array ();
  20. // Batch filter of banned words
  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. $ HitList2 = array ();
  33. Foreach ($ hitList as $ hit => $ type)
  34. {
  35. $ HitList2 [$ type] [] = $ hit;
  36. }
  37. Return $ hitList2;
  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, 300, $ max );
  52. }
  53. Return $ max;
  54. }
  55. /**
  56. * Block words by batch filtering
  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. // Get the banned 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, 50, '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 successful
  107. {
  108. $ HitList [$ hit] = $ point ['type'];
  109. }
  110. }
  111. Else
  112. {
  113. Break;
  114. }
  115. }
  116. Return $ hitList;
  117. }
  118. /**
  119. * Get the banned word tree 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, 300, $ wordTree );
  153. }
  154. Return $ wordTree;
  155. }
  156. }


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.