strpos($needle, $haystack) 未返回预期结果

问题描述 投票:0回答:2

我需要查看用户当前所在的 URL 是否与数据库中的 URL 紧密匹配(因此包含)。然而

strpos()
似乎没有正确完成这项工作。

这是我正在使用的代码

if (strpos($row['url'], $currenturl) === 0) {
    echo "YEAH!";
    return true;
}
echo "Current URL: " . $currenturl . " Permission URL: " . $row['url'] . " Strpos " . strpos($row['url'], $currenturl) . "<br>";

这是调试输出。

当前 URL:projects/projectview.php?whmcsid=0&id=4 权限 URL:projects/projectview.php?whmcsid=0 Strpos

所以它不匹配。有什么想法吗?

php strpos
2个回答
3
投票

您的参数顺序无效。

strpos
haystack
中搜索
needle
,所以:

if(strpos($currenturl, $row['url']) === 0) {
    // code here
}

-1
投票

要知道子字符串不存在,您必须使用,

   ===FALSE

as 0 也可以表示子串的起始位置。

© www.soinside.com 2019 - 2024. All rights reserved.