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?

Status
Not open for further replies.

buzzt

Programmer
Oct 17, 2002
171
CA
The following lines of code are part of a function to strip a list of cesored words from being written to my database.

$txt = preg_replace("#($find)\s#si", $replace." ", $txt);
$txt = preg_replace("#\s($find)\s#si", " ".$replace." ", $txt);
$txt = preg_replace("#\s($find)#si", " ".$replace, $txt);
$txt = str_replace(" " . $find, " " . $replace, $txt);
$txt = str_replace($find . " ", $replace . " ", $txt);
$txt = str_replace($find . "%", $replace . " ", $txt);

The problem is that if the first word in the submission is immediately followed by another character (such as a comma, asterisk, etc.) the censored word slides though. So far I cannot seem to fix this.

Any ideas?
 
Why don't you just use
$txt = preg_replace("/$find/i", $replace." ", $txt);

It removes all ocurrences of $find, no matter where.
 
$txt = preg_replace("'$find'si", $replace." ", $txt);
will work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top