Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Filter 2

Status
Not open for further replies.

Dustman

Programmer
May 7, 2001
320
US
Anybody got a good word filter already built? I can build one but don't want to if somebody's already done it. Just need a simple Word match (not part of a word, only the complete word) for use as a badword filter. -Dustin
Rom 8:28
 
Ok, I took some ideas from several other sources along with my own so props to the open source world!

[tt]function create_string($length) {
mt_srand((double)microtime() * 1000000);
$possible = '!@#$%^&*-';
$replace = false;
while(strlen($replace) < $length) {
$replace .= substr($possible, mt_rand( 0, strlen( $possible) - 1), 1);
}
return($replace);
}

function censor($message){
$fh = fopen(&quot;badwords.txt&quot;,&quot;r&quot;); //Open the wordlist

while($word = fgets($fh,4096)) {
$word = trim($word);
$message = preg_replace(&quot;/\b$word\b/i&quot;, create_string(strlen($word)),$message);
}
fclose($fh);
return $message;
}
[/tt]

The badwords.txt file contains a list of bad words, each word on a new line.

You can filter anything by calling censor().

Example:
If my badwords.txt was
dirt
mud
badword


And
[tt]
$message = &quot;The pig fell in the dirty mud.&quot;
$message = censor($message);
[/tt]

The final $message would be:
&quot;The pig fell in the dirty $&*.&quot; -Dustin
Rom 8:28
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top