PHP Classes

PHP Bad Word Filter: Check string to detect and remove bad words

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 129 All time: 9,337 This week: 164Up
Version License PHP version Categories
bad-words-filter 1.0Free To Use But R...5PHP 5, Text processing, Parsers


This class can be used to check string to detect and remove bad words.

It provides several functions for evaluating the presence of words that can be considered offensive or rude and eventually remove those words. Currently it can:

- Check if a word is in the list of known bad words
- Get an array with a list of bad words
- Replace bad words in a string with another given string

Picture of Meraj-Ul Islam
Name: Meraj-Ul Islam <contact>
Classes: 3 packages by
Country: Bangladesh Bangladesh
Age: 27
All time rank: 316543 in Bangladesh Bangladesh
Week rank: 358 Up12 in Bangladesh Bangladesh Up
Innovation award
Innovation award
Nominee: 1x


<!DOCTYPE html>
<html lang="en-US">
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Example of BadWordsFilter class</title>
body {
    background-color: silver;
    color: #000;
    padding: 6px;
h2 {
    color: green;
    text-decoration: underline;
h1 {
    text-align: center;
.title {
    color: blue;
.footer {
    /*color: grey;*/
    text-align: center;
    font-weight: bold;
    font-size: 1.5em;
    padding: 2px;
    margin: 2px;
    font-family: monospace;
    <h1>Examples of <span class="title">BadWordsFilter</span> class</h1>
// include the class file
// initialize the class
$filter = new BadWordsFilter();

// examples of isBadWord()
// check if a word is bad or not
echo '<h2>Usage example of isBadWord()</h2>';
$filter->isBadWord('suck')) {
'"suck" is a bad word!<br/>'; // this will be output
} else {
'"suck" is not a bad word!<br/>';
$filter->isBadWord('duck')) {
'"duck" is a bad word!<br/><br/>';
} else {
'"duck" is not a bad word!<br/><br/>'; // this will be output

// example of getList()
// get list of all bad words
echo '<h2>Usage example of getList()</h2>';
'Here are all bad words listed bellow:<hr/>';

// examples of getWords() and filterBadWords()
// get words from a sentence xD (string)
echo '<h2>Usage example of getWords() and filterBadWords()</h2>';
$words = $filter->getWords('Well this app sucks');
// print raw array
echo 'Words extracted from "Well this app sucks" (raw):<br/>';
'<br/>Words extracted from "Well this app sucks" and printed with filtering as sentence:<br/>';
// return the same string but with filtered words
echo $filter->filterBadWords($words);

// example of getWordlength()
// get any bad word's character length
echo '<h2>Usage example of getWordlength()</h2>';
'There are '.$filter->getWordlength('suck').' characters in the word "suck"<br/><br/>';

// example of filterBadWord()
// this is the most importannt function of the class
echo '<h2>Usage example of filterBadWord()</h2>';
'The word "suck" turns to "';
$filter->isBadWord('suck')) {
} else {
'" after filtering<br/><br/>';

// example of sanitizeWord()
// this is a helper function to get a alphanumeric word (string) from mixed character string
echo '<h2>Usage example of sanitizeWord()</h2>';
'The word "s-u-c-k" turns to "';
'" after sanitizing<br/><br/>';
<strong>Note:</strong> Please look at the source code for more details. Sorry for using bad words for examples. But as the purpose of demonstration I must use those. Otherwise it isn't possible to show a demo how it works. But I hope this class will help your site to maintain a clean environment.<br/><br/>
<div class="footer">Coded by Meraj-Ul Islam<br/>Contact:<br/>This file is free to use anywhere you want</div>

  Files folder image Files  
File Role Description
Plain text file BadWordsFilter.php Class The class file
Accessible without login Plain text file example.php Example Examples of all functions

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:9,337
This week:164Up