Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_replace & case sensitivity - Profanity Filter 1

Status
Not open for further replies.

jimoblak

Instructor
Oct 23, 2001
3,620
US
Is there a way to make preg_replace case insensitive?

I am working on a profanity filter and so far have the following:

$input=strtolower ($input);
$pattern = array ("/butt/","/poop/");
$replace = array ("rear","feces");
$output=preg_replace($pattern, $replace, $input);

I must drop the input to lower case so that 'Butt' or 'BUTT' is properly replaced.

The problem is that the entire string gets dropped to lower case: (Example: 'You are a BUTT!!!' becomes 'you are a butt!!!'). I would like to keep the safe words unchanged. Does anyone have an idea of how to pull this off?

Better yet... Can anyone point me to a good snippet for a profanity filter?
 
Use the "/i" modifier. The script:

Code:
<?php
print '<pre>';
$foo = &quot;aAaAa&quot;;
$bar = preg_replace ('/a/', 'B', $foo);
print $bar . &quot;\n&quot;;
$bar = preg_replace ('/a/i', 'B', $foo);
print $bar;
?>

Produces the output:
BABAB
BBBBB

If you have a list of proscribed words, you can hand them to preg_replace in an array. Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top