I am trying to find if any of the string inside an array exist in array of words.
For example:
$key = ["sun","clouds"];
$results = ["Sun is hot","Too many clouds"];
If any of $key
exists in $results
to return true
With below script I get the result I want but only if word in $results
is exact like the word in $key
.
function contain($key = [], $results){
$record_found = false;
if(is_array($results)){
// Loop through array
foreach ($results as $result) {
$found = preg_match_all("/\b(?:" .join($key, '|'). ")\b/i", $result, $matches);
if ($found) {
$record_found = true;
}
}
} else {
// Scan string to find word
$found = preg_match_all("/\b(?:" .join($key, '|'). ")\b/i", $results, $matches);
if ($found) {
$record_found = true;
}
}
return $record_found;
}
If the word in $results
is Enjoy all year round **sunshine**
my function returns false
as can not find word sun. How can I change my function to return true if string contain that string as part of word?
I want the word sun
to match even with sun
or sunshine
.
TIA