Profanity Filter PHP Function (1,700 bad words and profanities)

Following is a Profanity Filter PHP Function, which I am currently using to filter bad words from my forum posts. Click here to download my own list of over 1,700...

Following is a Profanity Filter PHP Function, which I am currently using to filter bad words from my forum posts.

Click here to download my own list of over 1,700 bad words and profanities: Click to Download (badwordsnew.rar).

Above list includes not only English bad words, but also profanities from other languages such as German, Dutch, French, Russian, etc. and it also includes all Google banned words I could collect from Internet.

Function works by replacing bad word with starred out version. So for example 'Damn' would become 'Dn' and 'Shoot' would become 'St' or 'Dang' would become 'Dg'. However, this function is smart enough to not to replace word 'Dangerous' with Dgerous'.

If you like it, or use it, or can make it better, please leave a comment.

function ReplaceBadWords($comment){ $badword = array(); $replacementword = array(); $wordlist = "badword1|badword2|badword3|badword4"; // replace with the list of bad words from attached rar file $words = explode("|", $wordlist); foreach ($words as $key => $word) { $badword[$key] = $word; $replacementword[$key] = addStars($word); $badword[$key] = "/\b{$badword[$key]}\b/i"; } $comment = preg_replace($badword, $replacementword, $comment); return $comment; }

function addStars($word) { $length = strlen($word); return substr($word, 0, 1) . str_repeat("*", $length - 2) . substr($word, $length - 1, 1); }