Page 1 of 2

How to replace forbiden words in lowercase or uppercase

Posted: Wed Dec 20, 2006 3:53 am
by dimitris
hi all,

i want to make $var = str_replace('DELETE','',$var);

work even when $var contains the token DELETE, delete, DeLeTe, Delete or another combination of lowercase and uppercase letters.

Do you have any suggestion such as using ereg_replace with a pattern?

Posted: Wed Dec 20, 2006 4:48 am
by Skittlewidth
if you are using PHP5 str_ireplace() is a case insensitive string replace function.

Posted: Wed Dec 20, 2006 5:11 am
by dimitris
Skittlewidth wrote:if you are using PHP5 str_ireplace() is a case insensitive string replace function.
Thanks!

Posted: Tue Dec 26, 2006 7:11 pm
by Z3RO21
If you don't have PHP 5 you could load a copy of the string into a temp variable. Then apply strtolower on the temp string and locate instances of 'delete' and make down the position in the string. Once you have a list of locations you can easiy filter out the words.

Posted: Tue Dec 26, 2006 7:37 pm
by RobertGonzalez
No temp variables are needed...

Code: Select all

<?php
$var = str_replace('DELETE', '', strtoupper($var)); 
?>
Keep in mind this is a complete variable replacement, meaning that if the var is delete, DELETE, or any other combination of upper or lower case letters forming the word DELETE, this will replace that variable with a blank string. This will not work on strings that contain multiple words or spaces around the word (unless you use trim()). For that you will need a regular expression replacement using something like preg_replace().

Posted: Tue Dec 26, 2006 8:23 pm
by John Cartwright
I would go down the preg_replace with the i modifier

Posted: Wed Dec 27, 2006 6:28 am
by Ollie Saunders
Jcart wrote:I would go down the preg_replace with the i modifier
I would too. You can use word break assertions then (\b). Without them you run the risk, as my friendly recently was the victim of, being kicked with the name 'badminton' when searching for string 'admin'.

Posted: Thu Dec 28, 2006 3:14 am
by Kieran Huggins
some other good examples I've seen recently: CampusCrew (screw) and msExchange (sexchange) :lol:

Posted: Thu Dec 28, 2006 10:34 am
by Ambush Commander
If you're going to use PCRE, you'll probably want to stick all of the naughty words in one expression so you're not running preg_replace a hundred plus times.

Posted: Thu Dec 28, 2006 11:36 am
by Ollie Saunders
Good idea, something like this:

Code: Select all

$words = array('scrote-um', 'pea-ness', 'vaj-china');
$wordPattern = '(';
foreach ($words as $v) {
    $wordPattern.= preg_quote($v, '~') . '|';
}
$wordPattern = ')';
$pattern= "~\b$wordPattern\b~i";
$filtered = preg_replace($pattern, $string, '<span title="I\'m naughty, are you naughty?">smurf</span>');

Posted: Thu Dec 28, 2006 11:49 am
by feyd
psst.. needs the trailing pipe trimmed off. ;)

Posted: Thu Dec 28, 2006 12:04 pm
by Ollie Saunders
feyd wrote:psst.. needs the trailing pipe trimmed off. ;)
crap. and i'm missing the dot here

Code: Select all

$wordPattern = ')';
well you get the picture anyway :)

Posted: Thu Jan 04, 2007 6:28 am
by Mordred
You need to repeat the filtering in a loop untill no more changes are made. Otherwise consider filtering the word "delete" from the string
"dedeletelete" (highlighted for clarity)

Posted: Thu Jan 04, 2007 11:51 am
by John Cartwright
Mordred wrote:You need to repeat the filtering in a loop untill no more changes are made. Otherwise consider filtering the word "delete" from the string
"dedeletelete" (highlighted for clarity)
The words aren't removed, they are replaced with smurf.

Posted: Thu Jan 04, 2007 12:09 pm
by Kieran Huggins
Jcart wrote:The words aren't removed, they are replaced with smurf.
Nice 8)