Check if string contains word in array

后端 未结 12 1516
旧巷少年郎
旧巷少年郎 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:05

    You can do the filter this way also

    $string = "This dude is a mothertrucker";
    if (preg_match_all('#\b(truck|shot|etc)\b#', $string )) //add all bad words here.       
        {
      echo "There is a bad word in the string";
        } 
    else {
        echo "There is no bad word in the string";
       }
    

提交回复
热议问题