Spam filtering is an important task for any application that deals with user-generated content, such as comments, forum posts, or emails. In this tutorial, we will see how to implement a simple spam filter using PHP.

The basic idea behind a spam filter is to analyze the content of a message and determine whether it is likely to be spam or not. There are several approaches that can be used for this purpose, ranging from simple keyword matching to complex machine learning algorithms. In this tutorial, we will use a simple keyword matching approach.

First, we need to define a list of spam keywords that will be used to identify spam messages. You can create this list manually by identifying common spam keywords or use a pre-existing list from reputable sources. For simplicity, let’s assume we have the following list of spam keywords:

$spamKeywords = [
‘buy now’,
‘click here’,
‘earn money’,
‘free trial’,
‘limited time offer’,
‘make money’,
‘you have won’,

Next, we need to implement a function that will check if a message contains any of the spam keywords. We can do this by iterating over the keywords and checking if each keyword is present in the message. Here’s an example implementation:

function isSpam($message, $spamKeywords) {
foreach ($spamKeywords as $keyword) {
if (stripos($message, $keyword) !== false) {
return true;
return false;

The `stripos()` function is used to perform a case-insensitive search for the keyword in the message. If a keyword is found, the function returns true, indicating that the message is likely to be spam. If none of the keywords are found, the function returns false.

To use the spam filter, you can call the `isSpam()` function with the message and the list of spam keywords. For example:

$message = “Congratulations! You have won a limited time offer. Click here to claim your prize.”;
if (isSpam($message, $spamKeywords)) {
echo “This message is likely to be spam.”;
} else {
echo “This message is not spam.”;

This will output “This message is likely to be spam.” since the message contains two spam keywords.

You can further improve the spam filter by implementing additional checks, such as detecting URLs, checking for excessive capitalization, or using a more advanced machine learning algorithm. However, even a simple keyword matching approach can be effective in catching a large number of spam messages.

It is important to note that a spam filter will never be 100% accurate and may occasionally classify legitimate messages as spam or vice versa. Therefore, it is a good practice to provide a way for users to report false positives or false negatives so that you can improve the spam filter over time.

In conclusion, implementing a spam filter is an essential part of any application that handles user-generated content. Using a simple keyword matching approach, we can identify spam messages based on the presence of known spam keywords.