View Single Post
  #3 (permalink)  
Old 03-13-10, 09:25 AM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
With a lot of bad words, this function can get consuming, but it does filter whole words only, and will not split words up.

Using wirehoppers example, with a litte changing around.

PHP Code:

<?php
/* Read in from the file here, not in the function - you only need to read the file once */
$wordlist[] = 'one';
$wordlist[] = 'two';
$wordlist[] = 'three';
$wordlist[] = 'four';

/* Sample data */
$words 'stone twoot three fourth under oven tree cookie oneder ream one sixone threetwo fournine four';

foreach (
$wordlist as $v)
      
$words clean($v,$words);

function 
clean($wordlist,$value)
{
        return 
preg_replace("/\b$wordlist\b/i"'***',trim($value));
}  

echo 
'Words: '.$words.PHP_EOL;
?>
Code:
Words: stone twoot *** fourth under oven tree cookie oneder ream *** sixone threetwo fournine ***
Reply With Quote