How to replace forbiden words in lowercase or uppercase
Moderator: General Moderators
How to replace forbiden words in lowercase or uppercase
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?
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?
- Skittlewidth
- Forum Contributor
- Posts: 389
- Joined: Wed Nov 06, 2002 9:18 am
- Location: Kent, UK
if you are using PHP5 str_ireplace() is a case insensitive string replace function.
Thanks!Skittlewidth wrote:if you are using PHP5 str_ireplace() is a case insensitive string replace function.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
No temp variables are needed...
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().
Code: Select all
<?php
$var = str_replace('DELETE', '', strtoupper($var));
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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>');- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
crap. and i'm missing the dot herefeyd wrote:psst.. needs the trailing pipe trimmed off.
Code: Select all
$wordPattern = ')';- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact: