Filtering Bad Language
If your attempting to create a family friendly website where you want respectable and appropriate posts, chances are your going to need a language filter. The code is actually quite simple. First of all, lets create the function.
<?php
function language_filter($string) {
$words = array("curse","word"," foul ","language");
foreach ($words as $curse) {
if (stristr(trim($string),$curse)) {
$length = strlen($curse);
for ($i = 1; $i <= $length; $i++) {
$stars .= "*";
}
$string = eregi_replace($curse,$stars,trim($string));
$stars = "";
}
}
return $string;
}
?>
Now lets break this down a little. The array $words will contain any word you wish to be filtered out. The function then goes through each word and checks if it is inside the string from the parameter. If it finds one, it then replaces the word with the appropriate number of asterisks. in order to actually use the filter, all you have to do is alter your current code slightly. For example:
<?php
$comment = $_POST['comment'];
echo $comment;
?>
Should now be:
<?php
$comment = language_filter($_POST['comment']);
echo $comment;
?>