I'am trying to build a PHP/MySQL/jQuery comment system. Off late I started to realize that manual spamming is a serious issue that need to be addressed carefully. So I thought of building a PHP function (initially, later can be implemented in OOPS concept) that will sniff the content and give points (spam points) based on some criteria. Getting a spam point less than will make the content inactive and send for moderation, whereas posts with spam points greater than 2 won't make the content eligible to be inserted in DB. This is a very basic piece of codes, so I want you to give me your valuable suggestion as how to make this code much better and if I am doing something wrong here.
<?php
#spam points < 2 { will be send for moderation}
#spam points > 2 { wont be posted , ie wont be inserted in DB}
function sniffSpams($content)
{
$spam_points = 0;
$url_pattern = '#(www\.|https?://)?[a-z0-9]+\.[a-z0-9]{2,4}\S*#i';
preg_match_all($url_pattern, $content, $matches, PREG_PATTERN_ORDER);
if(! empty($matches))
{
//url is/are present
$get_number_of_urls = count($matches[0]); //get the number of urls/emails present in content
if($get_number_of_urls > 2)
$spam_points += $get_number_of_urls; //1 point per link
else
{
$spam_words_uri = array('free', 'vote', 'play');
//if less thamn 2 , check for length of url
foreach ($matches[0] as $url)
{
if(strlen($url) > 150) //long url mostly are spam
$spam_points += 1;
foreach($spam_words_uri as $spam)
{
if(stripos($url, $spam) !== false )
$spam_points += 1;
}
}
}
}
$spam_words = array('Levitra', 'viagra', 'casino', '*');
foreach($spam_words as $spam)
{
if(stripos($content, $spam) !== false )
$spam_points += 1;
}
return $spam_points;
}
echo sniffSpams('the * dsjdjsd ');