Get complete CodeIgniter blog project for free!

Downloaded more than 161 times, this is full project must-study for serious developers!

By submitting this form, you agree to our Terms of Service and Privacy Policy.

Create spam filter in PHP

Spam is harmful to every website. Normally spam means link to share, slang, and other legal activities. Spam protector is very important for every website, we should check user comments. This article is about creating a spam filter in php. Php always help us to make something new. Php is a worldwide open-source programming language. Anyone can customize their php website’s script if they wish. This is a great feature of php. In this article, we are going to make this spam filter using php function. We create a function for detect spam and protect them. Create a function is spam($text) for detect spam. Then put spam words into this function.

function isspam($text)
$sfil[0] = "link";
$sfil[1] = "http";
$sfil[2] = "www";
$sfil[3] = "any slang";
$sfil[4] = "any word";
$sfil[5] = "any website";
$text = str_replace(" ", "", $text);
$text = strtolower($text);

$nosf = substr_count($text,$sfil[$i]);
return true;

return false;
We can increase spam list by increasing $sfil variable. Php substar_count function finds and detects spam word from our spam list in user input text. We also can create this function dynamically and we can use spam protect words dynamically. We can also create an advanced filter using some extra function. If we like that then we can use this function

function isspam($text,$user)
return false;
$string = $text;
$r1 = str_replace('~','',$string);
$r2 = str_replace('!','',$r1);
$r3 = str_replace('@','',$r2);
$r4 = str_replace('#','',$r3);
$r5 = str_replace('%','',$r4);
$r6 = str_replace('^','',$r5);
$r7 = str_replace('&','',$r6);
$r8 = str_replace('*','',$r7);
$r9 = str_replace('(','',$r8);
$r10 = str_replace(')','',$r9);
$r11 = str_replace('_','',$r10);
$r12 = str_replace('-','',$r11);
$r13 = str_replace('+','',$r12);
$r14 = str_replace('=','',$r13);
$r15 = str_replace('|','',$r14);
$r16 = str_replace('/','',$r15);
$r17 = str_replace(',','',$r16);
$r18 = str_replace('.','',$r17);
$r19 = str_replace('?','',$r18);
$r20 = str_replace('[','',$r19);
$r21 = str_replace(']','',$r20);
$r22 = str_replace('{','',$r21);
$r23 = str_replace('}','',$r22);
$r24 = str_replace('`','',$r23);
$r25 = str_replace(':','',$r24);
$r26 = str_replace('$','',$r25);
$r27 = str_replace('<','',$r26);
$r28 = str_replace('>','',$r27);
$r29 = str_replace('?','',$r28);
$r30 = str_replace(' ','',$r29);
$r31 = str_replace('&quot;','',$r30);
$r32 = str_replace('&amp;','',$r31);
$r33 = str_replace('\\\\','',$r32);
$str = strtolower($r33);
$res = mysql_query("SELECT word FROM spamword_table");
while ($row = mysql_fetch_array($res))
$sites[] = $row[0];
$nosf = substr_count($str,$sites[$i]);
return true;
return false;
This function at the first position we passed dual variable isspam($text,$user). $text for check user input and $user for check user who requests post new comment or new blog post. If this user admin then this function will return this function output as not spam though the input is spam word. In next step, we replaced some special character with an empty value for if user post a spam word with special character tricky then our advanced spam filter replace it and detect spam word strongly. Our function created and it’s ready to use. Now we are going to see how we use this function.
For that, we are going to create a demo script like it.
$input = [‘input’];
If (isspam($input))
echo ‘This input is spam’;

echo ‘This inpu is not spam!!’;
That’s so easy. I hope this article is helpful for you. I also hope you understand this article if you have any question about this article.

Search on PHPAns
Subscribe to PHPAns
Join with our 32000+ subscribers and get our latest articles in your email inbox for free.
Delivered by FeedBurner
We always respect your privacy and take protecting it seriously.
Connect Social With PHPAns