I'm trying to create a profanity filter for a shoutbox, however my code only removes one profanity, not multiple instances (kids have such bad language today 😉 )

this is the code:

$badWords = array('george', 'bush'); 
  foreach($badWords AS $badWord) { 
  $replacementWord = ""; 
     for($i=1; $i<= strlen($badWord); $i++) { 
     $replacementWord .= "*"; 
     } 
$filteredShout = str_replace($badWord, $replacementWord, $shout); 
 }

Thanks.

    This works for me:

    <?php
    
    $badWords = array('george', 'bush');
    
    function replacebadwords($string,$badword_array) {
    	foreach ($badword_array as $v) {
    		$newstring = "";
    		for($i = 0; $i < strlen($v); $i++) {
    			$newstring .= "*";
    		}
    	$string = str_replace($v,$newstring,$string);
    	}
    
    return $string;
    }
    
    $text = "My mom is george bush's grandma.";
    
    $text = replacebadwords($text,$badWords);
    
    echo $text;
    
    ?>
    

      hi, thanks for the reply but i've decided to use a much simpler method i've found, here it is:

      $badWords = array("george", "bush");
      $replWords  = array("******", "****",); 
      $filteredShout = str_replace($badWords, $replWords, $shout); 
      

        Even simpler:

        $badWords = array("george", "bush");
        $filteredShout = str_replace($badWords, '****' , $shout);
        

        The second argument can be a string even if the first argument is an array. The replacement string will be inserted in each instance where a member of the target array is found.

        Your first example had the advantage of matching the number of *'s to the legnth of the removed word. However, it didn't work because you did the replacement outside the foreach() loop.

        An enhancement: Maintain the banned words in an external file, one word per line. Load it with file(), which returns an array.

          no the str_replace is in the foreach loop he just changed the name of the var
          to $filteredShout when he should have kept it as $shout

          Originally posted by SeenGee

          $badWords = array('george', 'bush'); 
            foreach($badWords AS $badWord) { 
            $replacementWord = ""; 
               for($i=1; $i<= strlen($badWord); $i++) { 
               $replacementWord .= "*"; 
               } 
          $filteredShout = str_replace($badWord, $replacementWord, $shout); 
           }
          

          Thanks. [/B]

            Hi SeenGee

            These solutions will all work, I'm sure. I just wanted you to know that we have a profanity filter at work, but it's simple to circumvent. 1. You misspell the profanities (Jorge Boosh); 2. You add your own asterices (And f**k you too, matey); 3. You use foreign languages (That software you published last week is a load of merde.); 4. You do little word games (fut the shuck up).

            Profanity filters are loads of fun.

            Norm 🆒

              You'll want to use a case insensitive profanity filter, otherwise it will be stupid easy to break. So use regular expresions instead of str_replace. I hook mine into a db so words can be added i.e. the first time someone put in merde I would add it then it wouldn't get by anymore.

              something like this

              <?php
              //build the list of bad words
              $sql = "SELECT * FROM lang_filt";
              $query = mysql_query($sql);
              $badwords = array();
              while(($row = mysql_fetch_object($query)) !== FALSE)
                  $badwords[$row->word] = $row->replace;
              
              function filter($string,$words) {
                  foreach($words as $word => $replace) {
                      //replace instances of $word with a space after them
                      $string = eregi_replace($word . ' ',$replace,$string);
                      //replace instances of $word with a space before them
                      $string = eregi_replace(' ' . $word,$replace,$string);
                  } //end foreach
                  return $string;
              } //end filter
              ?>

              If you had the word ass replaced with but in your database then the following is what would happen to different strings.

              You're an ass. -> You're an but
              Suck my asshole. -> Suck my buthole
              Ass -> but
              aSs -> but
              asS -> but
              ASs -> but
              aSS -> but
              AsS -> but
              Please asses my program as soon as possible. -> Please butes my program as soon as possible.

              As you can see from the last example there is a weakness in this filetering scheme. But I can't remember off the top of my head how I fixed it. I think I make a list of accepted words, then did some sort of reversible suffling on the accepted words prior to removing the bad words then I unshuffled the accepted words.

                Originally posted by laserlight
                Well, there is [man]str_ireplace/man

                Yeah, but that's still going to have the weakness of replacing words that appear within words.

                asses, asset, etc.

                  Yeah, but that's still going to have the weakness of replacing words that appear within words.

                  Fair enough.

                  Just wondering though, do you prefer the Posix extended regex over Perl compatible regex?

                    Originally posted by laserlight
                    Just wondering though, do you prefer the Posix extended regex over Perl compatible regex?

                    Personally I prefer PCRE - for many of the reasons mentioned on the [man]regex[/man] page, and a few that aren't (e.g., there is no "ereg_callback()" function).

                      Originally posted by laserlight
                      Fair enough.

                      Just wondering though, do you prefer the Posix extended regex over Perl compatible regex?

                      It depends. I was a PERL programmer for many years. So if I already have the regex written in PERL I use prce so I can just bring my regex over, but if I don't already have one wirtten I find the posix quicker and easier to write and the performance hit has never been enough to be noticable in my applications. Though in all honesty I don't use regex that I don't already have written that often.

                        a year later

                        how would i set up one of these
                        do i makea php file and call it into the template i want it on
                        can someone explain how to actualy add it
                        please and thanx

                          Write a Reply...