Simple PHP strpos function not working, why?

青春壹個敷衍的年華 提交于 2019-11-26 12:29:57

When in doubt, read the docs:

[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) {

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

strpos() does not return true when it finds a match, it returns the position of the first matching string. Watchout, if the match is a the beginning of the string it will return an index of zero which will compare as equal to false unless you use the === operator.

Your failure condition is wrong.

strpos returns false if match is not found, so you need to explicitly check

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

Strpos always return position like you search "httpsL" in your string('https://google.com';) then it return 0th position and PHP evaluate it as false.

please see this link:(Hope its very usefull for you): http://php.net/manual/en/function.strpos.php

strpos

function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.

So I did like this

if (strpos($link, $unacceptable) !== false) {
    //Code
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!