PHP in_array查找任何匹配项 [英] PHP in_array Find Any Matches

查看:116
本文介绍了PHP in_array查找任何匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何匹配数组中的任何单词。例如,下面的代码用于在数组中查找测试名称,但在数组中找不到另一个测试名称(由于单词另一个)。

I am trying to figure out how to match any words within an array. For example, the code bellow works for finding "Test Name" within an array but does not find "Another Test Name" (due to the word "Another") within the array. Any ideas?

if (in_array($html[$i], $eventsarray))
{
    $topeventaa = "yes";
}
else
{
    $topeventaa = "no";
}


推荐答案

来自 http://php.net/manual/en/function.in-array.php

<?php

/**
 * Takes a needle and haystack (just like in_array()) and does a wildcard search on it's values.
 *
 * @param    string        $string        Needle to find
 * @param    array        $array        Haystack to look through
 * @result    array                    Returns the elements that the $string was found in
 */
function find ($string, $array = array ())
{       
    foreach ($array as $key => $value) {
        unset ($array[$key]);
        if (strpos($value, $string) !== false) {
            $array[$key] = $value;
        }
    }       
    return $array;
}

?>

这篇关于PHP in_array查找任何匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆