Skip to content
Advertisement

PHP strpos() not acting as expected

I am attempting to clean up my URLS in the database, however I am literally having a brain-fart. This is PHP 101 level stuff, but I seriously cannot figure out what’s wrong with the following snip:

JavaScript

When run .. I am expecting a “clean” example.com — However my output looks like:

JavaScript

Why is if (strpos($domain, "www.")){ not entering the if ?

Advertisement

Answer

Why not debug the code properly? strpos($domain, "www.") returns 0 as long as $domain starts with www, and that evaluates to false.

By properly checking whether strpos($domain, "www.") returns exactly false, you can fix your algorithm

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement