Update:
A solution independent of the word boundary would be to add spaces around the input line and the search word:
$str = ' ' . $str . ' '; function quote($a) { return ' ' . preg_quote($a, '/') . ' '; } $word_pattern = '/' . implode('|', array_map('quote', $array)) . '/'; if(preg_match($word_pattern, $str) > 0) { }
or by cyclizing the terms:
foreach($array as $term) { if (strpos($str, ' '. $term . ' ') !== false) {
Both can be placed in a function for ease of use, for example.
function contains($needle, $haystack) { $haystack = ' ' . $haystack . ' '; foreach($needle as $term) { if(strpos($haystack, ' ' . $term . ' ') !== false) { return true; } } return false; }
Look demo
Old answer:
You can use regular expressions:
function quote($a) { return preg_quote($a, '/'); } $word_pattern = implode('|', array_map('quote', $array)); if(preg_match('/\b' . $word_pattern . '\b/', $str) > 0) { }
The important part here is the \b boundary characters. You will only get a match if the value you are looking for is a (sequence) of words (words) per line.
source share