我用于过滤的这种贝叶斯方法在联系提交和报价表单请求方面取得了相当大的成功。该表格使用评分并处理来自世界各地的各种语言的请求。如果他们仅在各个领域未通过 3 或 4 次测试,那么我会将它们标记为垃圾邮件尝试。显然,像“123456”这样的电话号码会立即引发红旗。评论中的BBCode也是一个死赠品。
<?php
function nameCheck($var) {
$nameScore = 0;
//If name < 4 score + '3'
$chars_count = strlen($var);
$consonants = preg_replace('![^BCDFGHJKLMNPQRSTVWXZ]!i','',$var);
$consonant_count = strlen($consonants);
$vowels = preg_replace('![^AEIOUY]!i','',$var);
$vowel_count = strlen($vowels);
//We're expecting first and last name.
if ($chars_count < 4){
$nameScore = $nameScore + 3;
}
//if name > 4 and no spaces score + '4'
if (($chars_count > 4)&& (!preg_match('![ ]!',$var))){
$nameScore = $nameScore + 4;
}
if (($chars_count > 4)&&(($consonant_count==0)||($vowel_count==0))){
$nameScore = $nameScore + 5;
}
//if name > 4 and vowel to consonant ratio < 1/8 score + '5'
if (($consonant_count > 0) && ($vowel_count > 0) && ($chars_count > 4) && ($vowel_count/$consonant_count < 1/8)){
$nameScore = $nameScore + 5;
}
//Needs at least 1 letter.
if (!preg_match('![A-Za-z]!',$var)){
$nameScore = $nameScore + 10;
}
return $nameScore;
}
//added for testing
$var = $_GET['email'];
echo nameCheck($var);
?>
即使有人同花,我也会让它模仿我的尝试,这样我就可以修正我的得分。中文或韩文通常会有一些误报,但大多数情况下,任何用英文填写表格的人都会通过。像“无锡”这样的名字确实存在。