简单的PHP strpos函数不起作用,为什么? [英] Simple PHP strpos function not working, why?

查看:125
本文介绍了简单的PHP strpos函数不起作用,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why isn't this standalone code working:

$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 />';
        }
}

即使$link变量中包含https,每次都可以接受打印.

It's printing acceptable every time even though https is contained within the $link variable.

推荐答案

如有疑问,请阅读文档:

[strpos]返回大海捞针中第一次出现针的数字位置.

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

所以您想尝试更多类似的东西:

So you want to try something more like:

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

因为否则strpos返回一个数字,并且您正在寻找布尔值true.

Because otherwise strpos is returning a number, and you're looking for a boolean true.

这篇关于简单的PHP strpos函数不起作用,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆