Forgot Password / Register
Site Statistics
Total Members: 510
Total Tutorials: 242
Newsest User: Hp
Todays Unique Hits: 129
0 Users 3 Guests Online

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
1
2
3
4
CREATE TABLE `filter_new` (
  `word` varchar(50) NOT NULL,
  PRIMARY KEY (`word`)
) ENGINE=MyISAM;


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.
1
include("filter.php");


Then to use the filter, you simple need to run the filter() function we created.
1
$text = filter($text);


Change the varibles above to what you want to filter on your site.
DanielXP
Author:
Views:
1983
Rating:
Posted on Sunday 22nd June 2014 at 03:55 AM
DanielXP
DanielXP
I have updated this tutorial.

@SkillMaster, its a tutorial to be used on any system, you can replace the "DBconnect.php" used in the example for what file you need.
Posted on Thursday 21st June 2007 at 08:17 PM
SkillMaster
SkillMaster
ya and you would include config.php not DBconnect.php?
Posted on Thursday 21st June 2007 at 04:40 PM
DanielXP
DanielXP
In the database. You add the word in a new row.
Posted on Thursday 21st June 2007 at 04:14 PM
new2old
new2old
So where do you add the words?
I am really confused