douchao9899 2015-06-16 12:44
浏览 68
已采纳

如何检查数组中的项是否存在于字符串php中

I have an array of items, how do I scan a string and detect if the items in the array exist anywhere in the string. I seen other post that are similar to this question but none have worked. Maybe it is because I'm using wordpress

ex:

$string = get_the_title();// "The Man Who Wants You – Amos Lee [Vevo Official Video]" ---the string
$bads = get_the_tags();//  array('Amos Lee', 'Foo Fighters', 'U2'); array of items

foreach($bads as $bad) {
    $place = strpos($string, $bad);
    if (!empty($place)) {
        echo "True";
        exit;
    } else {
        echo "Not True";
    }
}
  • 写回答

2条回答 默认 最新

  • duanjiong1952 2015-06-16 12:48
    关注

    Try this, check if strpos doesn't return false:

    foreach($bads as $bad) {
        if (strpos($string, $bad) !== FALSE) {
            echo "True";
            exit;
        } else {
            echo "Not True";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部