从字符串数组中搜索关键字(PHP)

从字符串数组中搜索关键字(PHP),php,arrays,string,search,keyword,Php,Arrays,String,Search,Keyword,(PHP)搜索字符串中的关键字(从数组中)并打印一致,在这种情况下,所需的结果应返回“blue” 我该怎么做?使用以下方法: $array_keywords = ('red','blue','green'); $string = "Sometimes I'm happy, Sometimes I'm blue, Sometimes I'm sad"; 还可以使用和检查不区分大小写 或者您可以查看检查此代码 $array_keywords = array('red','blue','green'

(PHP)搜索字符串中的关键字(从数组中)并打印一致,在这种情况下,所需的结果应返回“blue

我该怎么做?

使用以下方法:

$array_keywords = ('red','blue','green');
$string = "Sometimes I'm happy, Sometimes I'm blue, Sometimes I'm sad";
还可以使用和检查不区分大小写

或者您可以查看检查此代码

$array_keywords = array('red','blue','green');

$string = 'Sometimes I'm happy, Sometimes I'm blue, Sometimes I'm sad';

foreach ($array_keywords as $keys) {
    if (strpos($string, $keys)) {
        echo "Match found"; 
        return true;
    }
}
echo "Not found!";
return false;

<?php
function strpos_array($haystack, $needles, &$str_return) {

    if ( is_array($needles) ) {
        foreach ($needles as $str) {
            if ( is_array($str) ) {
                $pos = strpos_array($haystack, $str);
            } else {
                $pos = strpos($haystack, $str);
            }

            if ($pos !== FALSE) {
                $str_return[] = $str;
            }
        }
    } else {
        return strpos($haystack, $needles);
    }
}

// Test
$str = [];
$array_keywords = ('red','blue','green');
$string = "Sometimes I'm happy, Sometimes I'm blue, Sometimes I'm sad";

strpos_array($string, $array_keywords,$str_return); 
print_r($str_return);
?>