An enhanced word filter for sNews, with whole words matching

I was doing a little troubleshooting today and noticed that the "bad words" filter in sNews was censoring out bits of words in the comments, i.e. charset became ch****t, obviously because the word arse is in my word filter file. Analysis becomes ****ysis, succumb becomes suc****b, etc etc.


bad wordsBasically, this is annoying as hell, so I have re-written the sNews cleanWords function to use only whole word matching instead. This is one of the easier mods to implement.

Simply BACK-UP your snews.php file, then rename the existing cleanWords function to cleanWordsORIG, now copy the following cleanWords function into your snews.php file, just above the closing PHP tags;

// CLEAN - WORD FILTER
function cleanWords($text) {
    $wordFilterFile = s('word_filter_file');
    if ((strtolower(s('word_filter_enable')) == 'on') && (file_exists($wordFilterFile))) {
        $bad_words_from_what = split("\n", file_get_contents($wordFilterFile));
        $bad_words_to_what = s('word_filter_change');
        for($x=0; $x< count($bad_words_from_what); $x++) {
            $text = preg_replace("/\b$bad_words_from_what[$x]\b/", $bad_words_to_what, $text);
        } 
       return $text;
    } else {
       return $text;
    }
}

Tada, you're done. If you're wondering how to enable word filtering, first you need to upload a bad words list (each word on a new line, like this one) to the same base directory as your snews.php file, then in the admin menu, select settings then comments, select the Enable Badwords filter box, then add the file name of the filter you uploaded, then what text you want to display instead of the offending word.

Share or Bookmark This Post:


Comments

RSS Comments Feed


No comments yet.




(optional, not publicly displayed)


(optional)

Subscribe

RSS Feed

Archives

Powered by HTML5

HTML5 Powered with CSS3 / Styling, Multimedia, and Semantics