Preg_replace whole word only

  • cesarcesar
  • Novice
  • Novice
  • cesarcesar
  • Posts: 23

Post 3+ Months Ago

Im trying to make a naughty word filter. It removes bad words fine, but instances where there is a bad word found in the text like "assist" and "asses" get caught in the filter as well. Strangely though if the sentence is: My asses to assist me." the clean version will read: My asses to ***ist me." It seems to clear the first use of the word in another word, but then blocks the rest. Any ideas? My script is below. Thanks.

Code: [ Select ]

function cleanWords($value) {

    /*  strip naughty words  */
    $bad_word_file = 'standards/badwords.txt';
    $strtofile = fopen($bad_word_file, "r");
    $badwords = explode("\n", fread($strtofile, filesize($bad_word_file)));
    fclose($strtofile);
    
    for ($i = 0; $i < count($badwords); $i++) {
        $wordlist .= str_replace(chr(13),'',$badwords[$i]).'|';
    }
    $wordlist = substr($wordlist,0,-1);

    $value = preg_replace("/\b($wordlist)\b/ie", 'preg_replace("/./","*","\\1")', $value);    
    return $value;

}
  1. function cleanWords($value) {
  2.     /*  strip naughty words  */
  3.     $bad_word_file = 'standards/badwords.txt';
  4.     $strtofile = fopen($bad_word_file, "r");
  5.     $badwords = explode("\n", fread($strtofile, filesize($bad_word_file)));
  6.     fclose($strtofile);
  7.     
  8.     for ($i = 0; $i < count($badwords); $i++) {
  9.         $wordlist .= str_replace(chr(13),'',$badwords[$i]).'|';
  10.     }
  11.     $wordlist = substr($wordlist,0,-1);
  12.     $value = preg_replace("/\b($wordlist)\b/ie", 'preg_replace("/./","*","\\1")', $value);    
  13.     return $value;
  14. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 119 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.