An enhanced word filter for sNews, with whole words matching

Comments (0)

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.

bookmark / share this: Bookmark and Share
rated 4/5 (1 vote)


Comments

No comments posted yet, why not be the first?


Write a comment

* = required field

:

:

:

:

You may insert urls in plain text, urls will be automatically linkified for trusted users and on seasoned posts only. All first comments are moderated, so use your email if you want to be remembered.


Back to top