如何检查数组中的项是否存在于字符串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";
}
}
我有一个项目数组,如何扫描字符串并检测数组中的项目是否存在于 字符串。 我看到其他帖子与这个问题类似,但没有一个有效。 也许是因为我正在使用wordpress p>
ex: h2>
$ string = get_the_title(); //“想要的人 你 - Amos Lee [Vevo官方视频]“---字符串
$ bads = get_the_tags(); //数组('Amos Lee','Foo Fighters','U2'); 项目数组
foreach($ bads as $ bad){
$ place = strpos($ string,$ bad);
if(!empty($ place)){
echo“True”;
退出;
}其他{
echo“Not True”;
}
}
code> pre>
div>
$ string = get_the_title(); //“想要的人 你 - Amos Lee [Vevo官方视频]“---字符串
$ bads = get_the_tags(); //数组('Amos Lee','Foo Fighters','U2'); 项目数组
foreach($ bads as $ bad){
$ place = strpos($ string,$ bad);
if(!empty($ place)){
echo“True”;
退出;
}其他{
echo“Not True”;
}
}
code> pre>
div>
答
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";
}
}
答
the return value of Strpos is false if the substring is not there, otherwise it will return the position of the substring therfore you have to check if the value is false or not.