Hi,

I was wondering on how to filter the words in your guestbook

comments or message board if some use typed a "bad/profanity" word and you need to replace the word with something like "****" when posted.

Example

"profanity word" would be replaced by **** 

I need some help using the ereg_replace function. 🙁

Anyone?

    • [deleted]

    A simple solution would be to use str_replace...

    <?
    $badwords = array ("bad","word","censor");
    $text = "I want to censor bad words";
    $text = str_replace($badwords,"!@#$%",$text);
    echo $text;
    ?>

      Better solution would be having the bad words in a TXT file which the script checks whether the word is in that file.

      badwords.txt

      BadWord1
      BadWord2
      BadWord3
      BadWord4
      BadWord5
      

      badwords.php

      <?
      $text = 'very BadWord1 ugly words which are so much BadWord2 BadWord3 that its so BadWord4';
      $file = file('badwords.txt');
      for($i = 0; $i < count($file); $i++)
      {
              $bdwrd1[] = substr($file[$i],0,-1);
              $bdwrd2[] = substr(str_repeat('*',strlen($file[$i])),0,-1);
      }
      $text = str_replace($bdwrd1,$bdwrd2,$text);
      echo $text;
      ?>
      
        Write a Reply...