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.

ScreenHunter 947 Nov. 21 10.10 How to Censor Words Using PHP<?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); 
?>

Incoming search terms for the article:

Related Posts

Turn A Photo Into A Vector Portrait With Photoshop

Correctly Embed Watermarks In Portrait And Landscape Photos Using Conditional Actions In Photoshop CS6.1

Oregon-Inspired Photoshop Tutorial

Create Google Play’s Tab Navigation Using jQuery And CSS

1 Comment

  1. Jleagle

    11.21.2012

    Wouldn’t using str_replace() be faster?