123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- /**
- * 过滤敏感词服务
- */
- namespace Common\Server;
- use Common\Cache\RedisCache;
- class SensitiveServer {
- /**
- * 敏感词过滤
- * @param $comment
- * @return mixed|string
- */
- public function filter_word($comment) {
- //$original_comment = $comment;
- //评论过滤特殊字符及空格后判断
- $strmap = array(
- '/à|á|å|â|ä/' => 'a',
- '/è|é|ê|ẽ|ë/' => 'e',
- '/ì|í|î/' => 'i',
- '/ò|ó|ô|ø/' => 'o',
- '/ù|ú|ů|û/' => 'u',
- '/ç|č/' => 'c',
- '/ñ|ň/' => 'n',
- '/ľ/' => 'l',
- '/ý/' => 'y',
- '/ť/' => 't',
- '/ž/' => 'z',
- '/š/' => 's',
- '/æ/' => 'ae',
- '/ö/' => 'oe',
- '/ü/' => 'ue',
- '/Ä/' => 'Ae',
- '/Ü/' => 'Ue',
- '/Ö/' => 'Oe',
- '/ß/' => 'ss',
- '/ /'=>' ',
- '/ /'=>'',
- '/~|·|!|@|#|¥|%|…|&|×|(|)|-|\+|=|『|【|』|】|、|:|;|“|”|\'|《|,|》|。|?|\/|—|_|:|√|<|°|丶|>|-|★|||│|‖|ˇ/'=>' ',
- '/[^\w\s\x80-\xff]/' => ' '
- );
- $comment = trim($comment);
- $comment = preg_replace(array_keys($strmap), array_values($strmap), $comment);
- $redis = new RedisCache();
- $sensitive_word_data = $redis->get('x_sensitive_word');
- if(empty($sensitive_word_data)) {
- $data = M('sensitive_word')->select();
- $sensitive_word_data = serialize($data);
- $redis->set('x_sensitive_word',$sensitive_word_data,2678400);
- $sensitive_word_data = $redis->get('x_sensitive_word');
- }
- $tmp_data = unserialize($sensitive_word_data);
- //die(json_encode($tmp_data));
- foreach($tmp_data as $value) {
- if(strpos($comment, $value['word'])!==false) {
- $comment = str_replace($value['word'], '***', $comment);
- }
- }
- return $comment;
- }
- }
|