Word Filter
In this tutorial we will create a basic word filter that can be used to remove any bad words a user may use on your website.
So let's begin with the SQL query.
Run this is phpMyAdmin
This table will hold all the words that shouldn't be used on your website.
I'm sure you can think of loads of words you can put in this table so I will not be listing any.
Now let's get to work with the filter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
include("DBConnect.php"); // Include which ever file is used for your database connection
// Create a new PHP Function with the name of "filter"
function filter($content) {
$filter = mysql_query("SELECT * FROM `filter`"); // Query `filter` table for bad words
while($wfilt = mysql_fetch_array($filter)) { // Loop the results
// Finds the string and replaces it with "(BadWord)"
$content = str_replace ("$wfilt[word]","(BadWord)",$content);
}
// Returns the "safe" string after being filtered
return $content;
}
?>
To use this filter you need to include the filter.php file into your script, like in the example below.
Then to use the filter, you simple need to run the filter() function we created.
Change the varibles above to what you want to filter on your site.