determine if a string contains one of a set of words in an array

I need a simple word filter that will kill a script if it detects a filtered word in a string.

say my words are as below

$showstopper = array(badword1, badword2, badword3, badword4);

$yourmouth = "im gonna badword3 you up";

if(something($yourmouth, $showstopper)){ 
//stop the show
}
Asked By: mrpatg
||

Answer #1:

You could implode the array of badwords into a regular expression, and see if it matches against the haystack. Or you could simply cycle through the array, and check each word individually.

From the comments:

$re = "/(" . implode("|", $showstopper) . ")/"; //  '/(badword1|badword2)/'
if (preg_match($re, $yourmouth) > 0) { die("foulmouth"); }
Answered By: Sampson

Answer #2:

in_array() is your friend

    $yourmouth_array = explode(' ',$yourmouth);
    foreach($yourmouth_array as $key=>$w){
       if (in_array($w,$showstopper){
         // stop the show, like, replace that element with '***'
         $yourmouth_array[$key]= '***';
       }
    }
$yourmouth = implode(' ',$yourmouth_array);
Answered By: pixeline

Answer #3:

You might want to benchmark this vs the foreach and preg_match approaches.

$showstopper = array('badword1', 'badword2', 'badword3', 'badword4');
$yourmouth = "im gonna badword3 you up";

$check = str_replace($showstopper, '****', $yourmouth, $count);
if($count > 0) { 
     //stop the show
}
Answered By: Gordon

Answer #4:

A fast solution involves checking the key as this does not need to iterate over the array. It would require a modification of your bad words list, however.

$showstopper = array('badword1' => 1, 'badword2' => 1, 'badword3' => 1, 'badword4' => 1);
$yourmouth = "im gonna badword3 you up";

// split words on space
$words = explode(' ', $yourmouth);
foreach($words as $word) {
    // filter extraneous characters out of the word
    $word = preg_replace('/[^A-Za-z0-9]*/', '', $word);
    // check for bad word match
    if (isset($showstopper[$word])) {
        die('game over');
    }
}

The preg_replace ensures users don't abuse your filter by typing something like bad_word3. It also ensures the array key check doesn't bomb.

Answered By: Corey Ballou

Answer #5:

not sure why you would need to do this but heres a way to check and get the bad words that were used

$showstopper = array(badword1, badword2, badword3, badword4);
$yourmouth = "im gonna badword3 you up badword1";

function badWordCheck( $var ) {

    global $yourmouth;
    if (strpos($yourmouth, $var)) {
        return true;
    }

}

print_r(array_filter($showstopper, 'badWordCheck'));

array_filter() returns an array of bad words, so if the count() of it is 0 nothign bad was said

Answered By: Galen
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles