...

View Full Version : Case sensitive



stophon4
05-03-2004, 08:10 PM
I have a badword-filter for a guestbook on my site, but I don't want to have to search for every possible comination of upper and lower case letters, so I need it to not be case sensitive, I cant convert the whole string to uppercase and lowercase since it then outputs it to the guestbook,
Does anyone know what to do?
I don't see why I should post my code, but if it will be useful then I will post it.

All help is greatly apreciated :thumbsup:

bcarl314
05-03-2004, 08:17 PM
function filterData($t) {
$r=stripslashes(htmlspecialchars($t,ENT_QUOTES));
$words = array("/sex/i","/love/i","/bad/i","/words/i","/here/i","/damn/i");
$replaceTxt = "[removed]";
foreach($words as $word) {
$r = preg_replace($word, $replaceTxt, $r);
}
return $r;
}

sidney
05-03-2004, 08:20 PM
create another var and convert it to upper or lower and test that in stead


$word="rudeword";
$testword=strtoupper($word);
if($testword=="RUDEWORD")
{
$word="******";
}

stophon4
05-03-2004, 09:29 PM
Thanks, it works great :thumbsup:
I never thought of doing that...

Mhtml
05-03-2004, 10:44 PM
Sidney, that would hardly work... wHaT iF i EnTeREd wOrdS lIKE tHIs?! The preg_replace with the pattern modifier is the better solution -- of course I'm lazy so I'd do it like -


preg_replace("/".$word."/i", $replaceTxt, $r);


You might want to throw checking for spaces around the word to, some legit words have filtered words in them ...


preg_replace("/\040".$word."\040/i", $replaceTxt, $r);


Or maybe you could use assertions...

bcarl314
05-03-2004, 10:54 PM
Or



preg_replace("/\b".$badWord."\b/i","[REMOVED]",$text);

sidney
05-04-2004, 12:27 AM
Sidney, that would hardly work... wHaT iF i EnTeREd wOrdS lIKE tHIs?! The preg_replace with the pattern modifier is the better solution -- of course I'm lazy so I'd do it like -


preg_replace("/".$word."/i", $replaceTxt, $r);


You might want to throw checking for spaces around the word to, some legit words have filtered words in them ...


preg_replace("/\040".$word."\040/i", $replaceTxt, $r);


Or maybe you could use assertions...

yes it would work as words like ThInK aGaIn would be converted to
think again or THINK AGAIN. so the test for banned words wold be in one case what ever was entered and no need to call the regexp engine

note i must add that i think any form of censorship is a horses arse

firepages
05-04-2004, 03:11 AM
a bit off topic but note that for PHP5 we will have str_ireplace (http://www.php.net/str_ireplace) for case insensitive replacements without the overhead of regex ... & a long time coming I must say ! ;)

stophon4
05-04-2004, 07:48 PM
a bit off topic but note that for PHP5 we will have str_ireplace (http://www.php.net/str_ireplace) for case insensitive replacements without the overhead of regex ... & a long time coming I must say ! ;)


Oh kool, well actually what it doesn't do a preg_replace, I just used parts of sydneys idea. What it actually does is it converts the current string to a diffrent string that is all lowercase, and after it searches the new converted to lower case string for and rude words, and if it finds them it doesn't post the other string and automatically bans them :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum