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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php