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.

Did you like this article? it will be appreciated if you share a coffee or burger with the author

Sent $5 to the author
Sent $10 to the author

Need Assistance?

I'm Sajjad Hossain, working on web application development since 2012. Do you need assistance on your project? or are you stuck with problems? I am available to help you.
If you want to contact with me ping me at -


We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy