Home  >  Article  >  Backend Development  >  php过滤广告内容(兼差,QQ号,淘宝兼职,网址)

php过滤广告内容(兼差,QQ号,淘宝兼职,网址)

WBOY
WBOYOriginal
2016-06-13 12:23:29861browse

php过滤广告内容(兼职,QQ号,淘宝兼职,网址)

如果你网站有评论那么你肯定会发现你网站经常会被一人注入广告了,如会有兼职,QQ号,淘宝兼职,网址信息了,下面我们就来看如何过滤这些内容吧。
?


用户发表的评论或者其他内容的广告的类型一般有下面的几种:

1:淘宝兼职 加QQ 123456789 群 (带qq号码或者微信号码或者其他数字号码)
2:taobao兼职,加QQ 号码 (带着英文的关键字)
3:淘宝兼职,加QQ ① ① ① ① ① ① (特殊数字的号码)
4:22222222 (全角类型的号码)

过滤的方法:
利用正则来匹配和替换字符串的标点符号,数字,字母,判断是否存在连续的数字或者关键字(支持全角和圆角),因为广告一般都是会带上qq号等联系方式。因此首先要对评论进行”净化”和替换,把全角的转换成半角的,去掉一些”沙子”,比如标点符号,空格,字母等等,只留下中文和数字。

例子:

$comment= “这$%是一个(1)8神器三四的网站,B快来加入④④呵@#呵 qq 1 2 3 4 5 6 7 8″;

1:”净化”内容,去除标点符号

$flag_arr=array('?','!','¥','(',')',':','‘','’','“','”','《','》',',','…','。','、','nbsp','】','【','~');?????????????

$comment=preg_replace('/\s/','',preg_replace("/[[:punct:]]/",'',strip_tags(html_entity_decode(str_replace($flag_arr,'',$comment),ENT_QUOTES,'UTF-8'))));
处理后,$comment 变成了:”这是一个(1)8神器三四的网站B快来加入①④呵呵qqq12345678″

2:其中可能参杂了一些全角的符号或者数字,所以利用下面的代码把全角符号转成正则可以匹配的半角

?$quanjiao = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4','5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E','F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O','P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T','U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y','Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd','e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i','j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n','o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z','(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[','】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']','‘' => '[', '\'' => ']', '{' => '{', '}' => '}', '《' => ' '>','%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.', ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|', '”' => '"', '\'' => '`', '‘' => '`', '|' => '|', '〃' => '"',' ' => ' ');

$comment=strtr($comment, $quanjiao);
php 的strtr 函数是用来转换字符串中特定的字符。
可以使用
strtr(string,from,to)
或者
strtr(string,array)

处理后,$comment变成了:”这是一个18神器三四的网站B快来加入①④呵呵qq12345678″;

3:评论里面可能 还包含有特殊字符(可以自己在下面的数组进行扩展新的特殊字符)

$special_num_char=array('①'=>'1','②'=>'2','③'=>'3','④'=>'4','⑤'=>'5','⑥'=>'6','⑦'=>'7','⑧'=>'8','⑨'=>'9','⑩'=>'10','⑴'=>'1','⑵'=>'2','⑶'=>'3','⑷'=>'4','⑸'=>'5','⑹'=>'6','⑺'=>'7','⑻'=>'8','⑼'=>'9','⑽'=>'10','一'=>'1','二'=>'2','三'=>'3','四'=>'4','五'=>'5','六'=>'6','七'=>'7','八'=>'8','九'=>'9','零'=>'0');
$comment=strtr($comment, $special_num_char);
处理后,$comment变成了:”这是一个18神器的网站B快来加入14呵呵qq12345678″;
如果评论里面出现繁体的数字,例如 ‘零’,’壹’,’贰’,’叁’,’肆’,’伍’,’陆’,’柒’,’捌’,’玖’,’拾’ 这些,一样的在上面的 $special_num_char 添加 和扩展就行。

4:评论里还可能出现正常的数字和汉字的数字混合,一样的用第3点的方法转换成正常的数字即可。

例子:这是一条广告qq 1二贰45六7899
转换后:
这是一条广告qq 1224567899

5:正则处理过滤广告

利用正则匹配?preg_match_all(‘/\d+/’,$comment,$match)
分析获取到的match[0] 匹配数组

foreach($match[0] as $val)//是否存在数字的qq号和微信号
{
??? if(strlen($val)>=6)
??? {//存在连续的长度超过6位的数字串,广告嫌疑很大
???????? $is_ad=true;
???????? break;
??? }
}
if(count($match[0])>=10)
{//间断的数字很多,存在广告的嫌疑
??? $is_ad=true;
}

ok,这样就可以判断内容是否广告,可以过滤大部分的常见的广告了

 $flag_arr=array('?','!','¥','(',')',':','‘','’','“','”','《','》',',','…','。','、','nbsp','】','【','~');        $comment=preg_replace('/\s/','',preg_replace("/[[:punct:]]/",'',strip_tags(html_entity_decode(str_replace($flag_arr,'',$comment),ENT_QUOTES,'UTF-8'))));        $quanjiao = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4','5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E','F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O','P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T','U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y','Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd','e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i','j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n','o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z','(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[','】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']','‘' => '[', '\'' => ']', '{' => '{', '}' => '}', '《' => '<','》' => '>','%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.', ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|', '”' => '"', '\'' => '`', '‘' => '`', '|' => '|', '〃' => '"',' ' => ' ');        $comment=strtr($comment, $quanjiao);        $special_num_char=array('①'=>'1','②'=>'2','③'=>'3','④'=>'4','⑤'=>'5','⑥'=>'6','⑦'=>'7','⑧'=>'8','⑨'=>'9','⑩'=>'10','⑴'=>'1','⑵'=>'2','⑶'=>'3','⑷'=>'4','⑸'=>'5','⑹'=>'6','⑺'=>'7','⑻'=>'8','⑼'=>'9','⑽'=>'10','一'=>'1','二'=>'2','三'=>'3','四'=>'4','五'=>'5','六'=>'6','七'=>'7','八'=>'8','九'=>'9','零'=>'0');        $comment=strtr($comment, $special_num_char);        preg_match_all('/\d+/',$comment,$match);        $is_ad = false;        foreach($match[0] as $val)//是否存在数字的qq号和微信号        {            if(strlen($val)>=6)            {//存在连续的长度超过6位的数字串,广告嫌疑很大                $is_ad=true;                break;            }        }        if(count($match[0])>=10)        {//间断的数字很多,存在广告的嫌疑            $is_ad=true;        }

?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn