Check if string contains word in array

后端 未结 12 1513
旧巷少年郎
旧巷少年郎 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 09:55

    1) The simplest way:

    if ( in_array( 'eleven',  array('four', 'eleven', 'six') ))
    ...
    

    2) Another way (while checking arrays towards another arrays):

    $keywords=array('one','two','three');
    $targets=array('eleven','six','two');
    foreach ( $targets as $string ) 
    {
      foreach ( $keywords as $keyword ) 
      {
        if ( strpos( $string, $keyword ) !== FALSE )
         { echo "The word appeared !!" }
      }
    }
    

提交回复
热议问题