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: 131 This week: 1All time: 9,328 This week: 560Up
Version License PHP version Categories
bad-words-filter 1.0Free To Use But R...5PHP 5, Text processing, Parsers
Description 

Author

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: 316443 in Bangladesh Bangladesh
Week rank: 416 Up9 in Bangladesh Bangladesh Up
Innovation award
Innovation award
Nominee: 1x

Example

<!DOCTYPE html>
<html lang="en-US">
<head>
<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>
<style>
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;
}
</style>
</head>
<body>
    <h1>Examples of <span class="title">BadWordsFilter</span> class</h1>
<?php
// include the class file
require('BadWordsFilter.php');
// initialize the class
$filter = new BadWordsFilter();

// examples of isBadWord()
// check if a word is bad or not
echo '<h2>Usage example of isBadWord()</h2>';
if(
$filter->isBadWord('suck')) {
    echo
'"suck" is a bad word!<br/>'; // this will be output
} else {
    echo
'"suck" is not a bad word!<br/>';
}
if(
$filter->isBadWord('duck')) {
    echo
'"duck" is a bad word!<br/><br/>';
} else {
    echo
'"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>';
echo
'Here are all bad words listed bellow:<hr/>';
print_r($filter->getList());
echo
'<hr/><br/>';

// 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/>';
print_r($words);
echo
'<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);
echo
'<br/><br/>';

// example of getWordlength()
// get any bad word's character length
echo '<h2>Usage example of getWordlength()</h2>';
echo
'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>';
echo
'The word "suck" turns to "';
if(
$filter->isBadWord('suck')) {
    echo
$filter->filterBadWord('suck','?');
} else {
    echo
'suck';
}
echo
'" 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>';
echo
'The word "s-u-c-k" turns to "';
print_r($filter->sanitizeWord('s-u-c-k'));
echo
'" 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: merajbd7@gmail.com<br/>This file is free to use anywhere you want</div>
</body>
</html>


  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  
 0%
Total:131
This week:1
All time:9,328
This week:560Up