gpt4 book ai didi

php - 简单的 PHP strpos 函数不起作用,为什么?

转载 作者:IT王子 更新时间:2023-10-28 23:58:56 25 4
gpt4 key购买 nike

为什么这个独立代码不起作用:

$link = 'https://google.com';
$unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png');

foreach ($unacceptables as $unacceptable) {
if (strpos($link, $unacceptable) === true) {
echo 'Unacceptable Found<br />';
} else {
echo 'Acceptable!<br />';
}
}

即使 https 包含在 $link 变量中,每次都可以打印。

最佳答案

如有疑问,请阅读 the docs :

[strpos] Returns the numeric position of the first occurrence of needle in the haystack string.

所以你想尝试更像:

// ...
if (strpos($link, $unacceptable) !== false) {

否则 strpos 将返回一个数字,而您正在寻找一个 bool 值 true

关于php - 简单的 PHP strpos 函数不起作用,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4858927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com