SensitiveServer.class.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * 过滤敏感词服务
  4. */
  5. namespace Common\Server;
  6. use Common\Cache\RedisCache;
  7. class SensitiveServer {
  8. /**
  9. * 敏感词过滤
  10. * @param $comment
  11. * @return mixed|string
  12. */
  13. public function filter_word($comment) {
  14. //$original_comment = $comment;
  15. //评论过滤特殊字符及空格后判断
  16. $strmap = array(
  17. '/à|á|å|â|ä/' => 'a',
  18. '/è|é|ê|ẽ|ë/' => 'e',
  19. '/ì|í|î/' => 'i',
  20. '/ò|ó|ô|ø/' => 'o',
  21. '/ù|ú|ů|û/' => 'u',
  22. '/ç|č/' => 'c',
  23. '/ñ|ň/' => 'n',
  24. '/ľ/' => 'l',
  25. '/ý/' => 'y',
  26. '/ť/' => 't',
  27. '/ž/' => 'z',
  28. '/š/' => 's',
  29. '/æ/' => 'ae',
  30. '/ö/' => 'oe',
  31. '/ü/' => 'ue',
  32. '/Ä/' => 'Ae',
  33. '/Ü/' => 'Ue',
  34. '/Ö/' => 'Oe',
  35. '/ß/' => 'ss',
  36. '/&nbsp;/'=>' ',
  37. '/ /'=>'',
  38. '/~|·|!|@|#|¥|%|…|&|×|(|)|-|\+|=|『|【|』|】|、|:|;|“|”|\'|《|,|》|。|?|\/|—|_|:|√|<|°|丶|>|-|★|||│|‖|ˇ/'=>' ',
  39. '/[^\w\s\x80-\xff]/' => ' '
  40. );
  41. $comment = trim($comment);
  42. $comment = preg_replace(array_keys($strmap), array_values($strmap), $comment);
  43. $redis = new RedisCache();
  44. $sensitive_word_data = $redis->get('x_sensitive_word');
  45. if(empty($sensitive_word_data)) {
  46. $data = M('sensitive_word')->select();
  47. $sensitive_word_data = serialize($data);
  48. $redis->set('x_sensitive_word',$sensitive_word_data,2678400);
  49. $sensitive_word_data = $redis->get('x_sensitive_word');
  50. }
  51. $tmp_data = unserialize($sensitive_word_data);
  52. //die(json_encode($tmp_data));
  53. foreach($tmp_data as $value) {
  54. if(strpos($comment, $value['word'])!==false) {
  55. $comment = str_replace($value['word'], '***', $comment);
  56. }
  57. }
  58. return $comment;
  59. }
  60. }