Page 1 of 1
Foul Words Filtering
Posted: Tue Feb 14, 2006 5:39 am
by khaki_monster
hi guyz!
is body knows filtering?i need some tips regarding how to filter such foul words.
help here please...
cheerz!
Posted: Tue Feb 14, 2006 5:57 am
by jayshields
I'm sure Google would return alot of results, as would searching this forum.
As a starting point,
str_replace would suffice for replacing specific words.
Code: Select all
$badwords = array(
'blah',
'blah2',
'blah3'
);
str_replace($badwords, 'xxx', $string);
Posted: Tue Feb 14, 2006 6:02 am
by matthijs
A while ago I made these snippets:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// i use an array here because of the way I wanted to implement it. You could change it to a string of course
$input = array ('Badwords are in this language',
'more bad words in this sentence',
'and even more bad words');
$string = implode("\r\n", $input);
$badwords = array();
$badwords = array('bad','word', 'fu**');
function checkforbadwords($badwords,$string) {
foreach ($badwords as $key => $value)
{
if(stristr($string, $value) === FALSE)
{
//echo '<p>'. $value . ' not found in $string</p>';
return false;
}
else
{
//echo '<p>'. $value . ' found in string</p>';
return true;
}
}
}
if(checkforbadwords($badwords,$string)) { echo "Bad words found"; }
?>
</body>
</html>
and
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
echo '<p>$input is: </p>';
$input = array ('Badwords are in this language',
'more bad words in this sentence',
'and even more bad words');
print_r($input);
echo '<hr>';
function word_filter3 ($array,$badwords) {
$clean = array();
foreach ($array as $val)
{
$clean[] = str_replace($badwords,"***",strtolower($val));
}
return $clean;
}
$badwords = array();
$lines = file("bad_words.txt");
echo '<p>Bad words file contains: </p>';
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
$badwords[] = trim($line);
}
$filteredinput = word_filter3($input,$badwords);
foreach ($filteredinput as $key => $val)
{
echo '<p>After Word_filter3: ' . $val .'</p>';
}
?>
</body>
</html>
the bad_words.txt is:
Code: Select all
curse
foul
language
fu**
bad
words
Maybe this gives you some ideas?
[edit: while previewing my post I saw that the forum software filters as well

Maybe you could have a look have phpBB does it?]