Although above replies are a quick fix, this seems more usable:
$replacement = '*';
$bad_words = array('badword1', 'badword2');
// a simple array which easily can be extended.
// proto bool contains_badwords(reference string $string)
function contains_badwords(&$string) {
// Make the argument a reference, so we can
// change the original string.
global $bad_words, $replacement;
$result = false;
$s_string = strtolower($string);
foreach($bad_words as $badword) {
$badword = strtolower($badword);
if (strpos($s_string, $badword) !== false) {
/ notice the '!==' operator, which means
equal in value AND equal in type, strpos()
might return 0, which is evaluated as false/
$string = eregi_replace($badword,str_repeat($replacement, strlen($badword)), $string);
$result = true;
/ We changed $string here, because we modified
s_string to be the lowercase version for easy checking.. /
}
}
return $result;
}
/ example usage /
$comment = $HTTP_POST_VARS['comment'];
if (contains_badwords($comment)) {
$user->karma--;
$user->write_to_db();
}
Above code changes all badwords that are in the array
$badwords into asterixes..
Mathieu Kooiman