douchao9899 2015-06-16 20:44
浏览 67
已采纳

如何检查数组中的项是否存在于字符串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条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭