douchao9899 2015-06-16 20: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 20: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条)

报告相同问题?