Foul Words Filtering

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Foul Words Filtering

Post by khaki_monster »

hi guyz!

is body knows filtering?i need some tips regarding how to filter such foul words.

help here please...

cheerz!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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);
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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?]
Post Reply