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:
<?php
$domain = 'http://www.example.com';
echo "\n\n\e[31mBeginning Domain is: $domain\n";
if (strpos($domain, '//')){
$domain = explode( '//', $domain)[1];
echo "\n\e[33mRemoving HTTP\n";
}
echo "\n\n\e[35mAfter first IF domain is: $domain\n";
if (strpos($domain, "www.")){
echo "\n\e[33mREMOVING WWW\n";
$domain = explode( "www.", $domain)[1];
}
echo "\n\n\e[35mAfter second IF domain is: $domain\n";
die("\n\n\e[31m$domain\n\n");
When run .. I am expecting a "clean" example.com
-- However my output looks like:
Beginning Domain is: http://www.example.com
Removing HTTP
After first IF domain is: www.example.com
After second IF domain is: www.example.com
www.example.com
Why is if (strpos($domain, "www.")){
not entering the if
?
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