Adirondack Web Services AWS: Growing Business with Technology Home  |  About Us  |  Contact  | 
 
AWS Internet Home Check Your AWS Webmail Contact Adirondack Web Services View this page in printer-friendly format Email this page to a friend!   For sales call toll-free 1.866.736.8531
Sun February 05, 2012
Home

Articles

Services

  Copy Writing

  Custom Programming

  Database Development

  E-Commerce Design

  Flash Development

  Graphic Design

  Internet Access

  Internet Marketing

  Live Chat

  Network Design

  Phone Services

  Real Estate Solutions

  Site Tracker

  Software Development

  Web Design

  Web Hosting

  Website Management

About AWS

Portfolio

Contact

Newsletter

Help & Demos

Testimonials

Links


AWS RSS Feed GeoURL


Visa, MasterCard and American Express Accepted
 
Instant Message AWS
 

 
Author Image Author Information
Jasen Lawrence
Programming, Web Design
jasen@awsinternet.com
Jasen Lawrence joined the team in August of 2004 and is the resident coder and back-end backbone of AWS. Schooled at Juniata College in Pennsylvania and by many caffeinated hours o... [more about this author]


A PHP Bad Word / Vulgar Word Filter Script
December 1st, 2005, 6:57pm CST AWS RSS Feed View This Article In PDF Format.

 

This is a simple file based bad word filter PHP function. The function offers a variety of options. You can choose how many letters of the bad word you wish to keep, which character you want to use as the badword "bleeper" as well as a rating for the bad word.
The bad words file would be formatted like this:
9badword
2anther badword
7A bad word Phrase
7YetAnotherWord

The first character number is the bad word "rating" the higher the number the worse the word. This can be useful in say, a forum situation where a user can specify their bad word tolerance. So if I pass the function a bad word tolerance of 5, the function will only remove words with a tolerance rating higher than 5.
A sample function call would look like this:

$myString "This is a sentence with some badwords, It is an example based on the Yetanotherword file above.";
echo 
filterBadWords($myString,"badwords.txt","*",1,3);

If our above badwords file was used for this string the output would result in:
This is a sentence with some b******s, It is an example based on the Y************* file above.

Here is the function code:

<?PHP
    
function filterBadWords($str,$badWordsFile,$replaceChar="*",$showLetters=0,$rating=0){
            
// check for the badwords file
        
if(!is_file($badWordsFile)){
            echo 
'ERROR: Could not find badwords file "'.$badWordsFile.'"';
            exit;
        }
        else{
                
//open or file as resource $handle
            
$handle =  fopen($badWordsFile,"r");
        }
            
// while we're not at eof (End Of File) do this
        
while(!feof($handle)){
            
$badword trim(fgets($handle)); // get the word from the file
            
$word_rating substr($badword,0,1); // get the badword "rating", word is in format 5myBadWord
            
$badword substr($badword,1);  //take the rating off of the badword so we have just the badword left
            
                //if my word rating is greater than my exceptable level bleep it out
            
if($word_rating>$rating){
                    
// look for and take out our bad word
               
$str eregi_replace($badwordsubstr($badword,0,$showLetters).sprintf("%'".$replaceChar.(strlen($badword)-$showLetters)."s"NULL), $str);
            }
        }
        
//return our formatted string
        
return $str;
    }
    
?>

 

Perma-Link: http://www.awsinternet.com/articles/2005/PHP_Bad_word_vulgar_word_filter.html

Related Links:
http://www.gr0w.com/articles/code/php_bad_words_filter/index.php
Site © 2001 - 2012 Adirondack Web Services. Site designed, maintained, and hosted by AWSInternet. -
Built in 0.0348799 secs.   Currently 4 guest(s) on-line.