views:

30

answers:

2

I asked this question yesterday: http://stackoverflow.com/questions/3495735/compare-array-of-words-to-a-textarea-input-with-javascript

Now I want to do the same thing with php...

Is there any easy code for this?

Thanks

UPDATE: I would like to test the textarea input against the array, and if match of bad words found, die();

Thanks

+1  A: 
$bad_words = array('first','second','third');

$posted = str_ireplace($bad_words, '****', $posted);

This will replace bad words with '**'

EDIT:

to check if any of words from bad_words exist in a string:

foreach( $bad_words as $bad ){
 if( stristr($posted, $bad) !== FALSE )
 {
  $contains_bad_words = TRUE;
 }    
}
Kemo
actually i was looking for an if statement where I cancel the entire operation if bad words was found. thanks alot though
Camran
Also, any way of making this case INsensetive?
Camran
What I mean is to make the match if "Bad" "BAD" "bad" when only "bad" is in the array...
Camran
+1  A: 

You could try something like this:

$bad_words = array('ring','sarah','chuck');

$intersect = array_intersect(explode(' ', strtolower($_POST['textarea'])), $bad_words);

if(count($intersect)) die('You should wash your mouth out with soap!');

The array_intersect will compare the two different arrays of words and return all of the values that exist in both arrays. Therefore if count($intersect) is anything but 0 (evaluated as a false in this case) then you can exist the script and output an error.

Noah Goodrich