Jump to content

[SOLVED] Offensive word/phrase filter


bdichiara

Recommended Posts

I've got a function that is being used to filter messages before they're inserted into a database that gets rid of any foul language and replaces it with either **** or an alternate word/phrase. The problem is it replaces parts of other words. For example, the word Assault gets replaced as: ***ault. Can someone please help me modify this so it only replaces whole words? I was thinking maybe putting spaces around the word to check it, but this will cause it to fail on words at the beginning of the text or with punctuation after or around the word (like "bleep" or bleep.)  here's the function:

function filterContent($content){
   $filename = "../../foul_language.txt";
   if(file_exists($filename)){
      $fh = @fopen($filename, 'r');
      $obscenities = @fread($fh, filesize($filename));
      $obscenities = explode("\n",$obscenities);
      
      $content = trim($content);

      foreach ($obscenities as $curse_word) {
         $curse_word = trim($curse_word);
         $curse_word = explode(";",$curse_word);
         $word = $curse_word[0];
         $replace = $curse_word[1];
         if (stristr($content,$word)){
            if(empty($replace)){
               $length = strlen($word);
               $replace = str_repeat("*",$length);
            }
            
            // Replace the dirty string with the filtered string
            $content = eregi_replace($word, $replace, $content);
         }
         $replace = "";
      }
   }
   
   return $content;
}

And my file is setup like this:

word;
word;alternate

if it's blank after the ";", it uses *****. Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/69909-solved-offensive-wordphrase-filter/
Share on other sites

i'm confused. I tried using this, but when it matches, it just returns an entire blank line:

 

$expr = '\b$word\b';
$content = preg_replace($expr,$replace,$content);

also tried this:

$expr = '\b'.$word. '\b';

 

And I think it's blank because our php display errors are turned off... and unforturtunately, it stopped logging them in the error log a few months ago and I can't figure out why. but that's another story.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.