Check if string contains word in array

后端 未结 12 1335
旧巷少年郎
旧巷少年郎 2020-12-01 09:12

This is for a chat page. I have a $string = \"This dude is a mothertrucker\". I have an array of badwords: $bads = array(\'truck\', \'shot\', etc)

相关标签:
12条回答
  • 2020-12-01 10:12

    You can flip your bad word array and do the same checking much faster. Define each bad word as a key of the array. For example,

    //define global variable that is available to too part of php script
    //you don't want to redefine the array each time you call the function
    //as a work around you may write a class if you don't want global variable
    $GLOBALS['bad_words']= array('truck' => true, 'shot' => true);
    
    function containsBadWord($str){
        //get rid of extra white spaces at the end and beginning of the string
        $str= trim($str);
        //replace multiple white spaces next to each other with single space.
        //So we don't have problem when we use explode on the string(we dont want empty elements in the array)
        $str= preg_replace('/\s+/', ' ', $str);
    
        $word_list= explode(" ", $str);
        foreach($word_list as $word){
            if( isset($GLOBALS['bad_words'][$word]) ){
                return true;
            }
        }
        return false;
    }
    
    $string = "This dude is a mothertrucker";
    
    if ( !containsBadWord($string) ){
        //doesn't contain bad word
    }
    else{
        //contains bad word
    }
    

    In this code we are just checking if an index exist rather than comparing bad word with all the words in the bad word list.
    isset is much faster than in_array and marginally faster than array_key_exists.
    Make sure none of the values in bad word array are set to null.
    isset will return false if the array index is set to null.

    0 讨论(0)
  • 2020-12-01 10:12

    Put and exit or die once it find any bad words, like this

    foreach ($bads as $bad) {
     if (strpos($string,$bad) !== false) {
            //say NO!
     }
     else {
            echo YES;
            die(); or exit;            
      }
    }
    
    0 讨论(0)
  • 2020-12-01 10:15

    I would go that way if chat string is not that long.

    $badwords = array('motherfucker', 'ass', 'hole');
    $chatstr = 'This dude is a motherfucker';
    $chatstrArr = explode(' ',$chatstr);
    $badwordfound = false;
    foreach ($chatstrArr as $k => $v) {
        if (in_array($v,$badwords)) {$badwordfound = true; break;}
        foreach($badwords as $kb => $vb) {
            if (strstr($v, $kb)) $badwordfound = true;
            break;
        }
    }
    if ($badwordfound) { echo 'Youre nasty!';}
    else echo 'GoodGuy!';
    
    0 讨论(0)
  • 2020-12-01 10:15
     $string = "This dude is a good man";   
     $bad = array('truck','shot','etc'); 
     $flag='0';         
     foreach($bad as $word){        
        if(in_array($word,$string))        
        {       
            $flag=1;       
        }       
    }       
    if($flag==1)
      echo "Exist";
    else
      echo "Not Exist";
    
    0 讨论(0)
  • 2020-12-01 10:16

    There is a very short php script that you can use to identify bad words in a string which uses str_ireplace as follows:

    $string = "This dude is a mean mothertrucker";
    $badwords = array('truck', 'shot', 'ass');
    $banstring = ($string != str_ireplace($badwords,"XX",$string))? true: false;
    if ($banstring) {
       echo 'Bad words found';
    } else {
        echo 'No bad words in the string';
    }
    

    The single line:

    $banstring = ($string != str_ireplace($badwords,"XX",$string))? true: false;
    

    does all the work.

    0 讨论(0)
  • 2020-12-01 10:17

    can you please try this instead of your code

    $string = "This dude is a mothertrucker";
    $bads = array('truck', 'shot');
    foreach($bads as $bad) {
        $place = strpos($string, $bad);
        if (!empty($place)) {
            echo 'Bad word';
            exit;
        } else {
            echo "Good";
        }
    }
    
    0 讨论(0)
提交回复
热议问题