简体   繁体   中英

compare “array of words” against textarea with php

I asked this question yesterday: 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

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.

$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;
 }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM