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


How do I check if items in a array are present in a string php

我有一个项目数组,如何扫描字符串并检测数组中的项目是否存在于字符串中的任何位置。我看到了其他与此问题类似的帖子,但没有一个有效。也许是因为我正在使用wordpress。

前任:

$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";
    }
}

试试这个,检查strpos是否不返回 false:

foreach($bads as $bad) {
    if (strpos($string, $bad) !== FALSE) {
        echo "True";
        exit;
    } else {
        echo "Not True";
    }
}

如果子字符串不存在,则 Strpos 的返回值为 false,否则它将返回子字符串的位置,因此您必须检查该值是否为 false。