Simple PHP strpos function not working, why? Simple PHP strpos function not working, why? php php

Simple PHP strpos function not working, why?


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