Update:
A word boundary independent solution would be to add spaces around the input string and the search words:
$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 looping over the terms:
foreach($array as $term) {
if (strpos($str, ' '. $term . ' ') !== false) {
// word contained
}
}
Both can be put in a function to simplify the use, e.g.
function contains($needle, $haystack) {
$haystack = ' ' . $haystack . ' ';
foreach($needle as $term) {
if(strpos($haystack, ' ' . $term . ' ') !== false) {
return true;
}
}
return false;
}
Have a look at a DEMO
Old answer:
You could 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 are the boundary characters \b here. You will only get a match if the value you search for is a (sequence of) word(s) in the string.