How to Censor Words Using PHP
Here’s a pretty neat and simple PHP snippet from PHPSnips that offers you a way to censor any word and then replace it with another word or string. Just provide a list of words you want censored out and let the code do the rest.
This is definitely useful for BBS or forum sites, chat rooms, and perhaps even in a blog’s comment sections.
<?php function censorWords($text){ $find = array( '/damn/i', '/shit/i', '/fuck/i' ); $replace = array( 'dang', 'shoot', 'frick' ); return preg_replace($find,$replace,$text); } $text = 'That Damn cat is fucking stupid.'; echo censorWords($text); ?>
1 Comment
Jleagle
11.21.2012
Wouldn’t using str_replace() be faster?
There are no trackbacks to display at this time.