note 45213 deleted from function.preg-replace by aidan

From: Date: Mon, 30 Aug 2004 07:37:48 +0000
Subject: note 45213 deleted from function.preg-replace by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75653@lists.php.net to get a copy of this message
Note Submitter: lgomez edgeracing com ---- Here's a way of replacing any collection of words from a string. Good for filtering bad words. I don't know if it can be compacted even more. <?php function filter($str,$words,$replacement='*'){ foreach($words as $word){ $str=preg_replace("/\\b$word\\b/i",' '.str_repeat($replacement,strlen($word)).' ',$str); } return $str; } $bad_words=array('butt','poop','crap'); $text='One thing to take note bhjbdscrapmkjv of is that if you use poop as paint, you may not get the crap you expect butt.'; echo filter($text,$bad_words); ?>

« previous php.notes (#75653) next »