Search code examples
phpstrpos

Strpos always gives true


I have two type of links which are strings taken from database:

http://www.website.com/anything-else.html
www.website.com/anything-else.html

I need ALL links to be displayed with http:// no matter what so Im using this simple code to determine whether link has http in it and if not add it:

if (strpos($links, 'http') !== true) {
    $linkai = 'http://'.$links;
}

The problem is, it is adding http:// to any link no matter if it has it or not. I tried ==false ect. Nothing works. Any ideas?


Solution

  • Try this

    if (strpos($links, 'http') === false) {
       $linkai = 'http://'.$links;
    }
    

    In strpos documentation says return value not Boolean always.

    "Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function."