Check if string contains word in array [duplicate] Check if string contains word in array [duplicate] arrays arrays

Check if string contains word in array [duplicate]


function contains($str, array $arr){    foreach($arr as $a) {        if (stripos($str,$a) !== false) return true;    }    return false;}


1) The simplest way:

if ( in_array( 'three',  ['one', 'three', 'seven'] ))...

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 !!" }  }}


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