Re: adult content censoring
| From: | Richard Davey | Date: | Sun, 25 Apr 2004 14:47:31 +0000 |
| Subject: | Re: adult content censoring | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-184561@lists.php.net to get a copy of this message | ||
Hello Andy,
Sunday, April 25, 2004, 3:22:29 PM, you wrote:
AB> i have to make an adult content censoring system so people cant post "bad"
AB> words on any public viewable posts. i know preg_replace and possibaly
AB> preg_match would be a huge help but im wondering how i would put all the bad
AB> words in a file (textfile) 1 word on a line and have it look through the
AB> file for the words? when it finds a match then it will either replace the
AB> word with * but better yet tell the user that content in the post was
AB> rejected because of bad content and take them back to the form with the
AB> stuff they typed in it...
The way I do it on a piece of forum software I wrote on a VERY popular
web site was as follows:
Create an include file which contains all your badwords in an array,
like:
$badword[] = 'well';
$badword[] = 'you';
$badword[] = 'get';
$badword[] = 'the';
$badword[] = 'idea';
Then I have a simple function as follows:
function APL_Func_BadWord ($text)
{
global $badwords;
// This will check our given text against the badword list. It does it slightly
differently
// in that instead of firing up the regular expression engine, we use a more
simple but just as effective technique:
// Get the length of the text
$original_length = strlen($text);
// Now strip out ANY of the "bad words" using the fast str_replace
and count the result
$stripped_length = strlen(str_replace($badwords, '', strtolower($text)));
// In PHP5 we will use the str_ireplace function instead of needing strtolower:
// $stripped_length = strlen(str_ireplace($badwords, '', $text));
// If the two lengths are different, the pos contained a badword - simple as
that!
if ($original_length <> $stripped_length)
{
return true;
}
else
{
return false;
}
}
and that's it - I pass any string I need to this whenever someone
posts a message, subject, etc etc and it hasn't failed me yet :)
(unless the bad word isn't in your list of course!)
--
Best regards,
Richard Davey
http://www.phpcommunity.org/wiki/296.html