13 June 2010

bad word filter script in php

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:

  • 9   badword
  • 2   anther badword
  • 7   A bad word Phrase
  • 7   YetAnotherWord
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:

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.'"';
  else {
    //open or file as resource $handle
    $handle =  fopen($badWordsFile,"r");
  // while we're not at eof (End Of File) do this
    $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
      // look for and take out our bad word
      $str = eregi_replace($badword, substr($badword,0,$showLetters).sprintf("%'".$replaceChar.(strlen($badword)-$showLetters)."s", NULL), $str);
  //return our formatted string
  return $str;