I am trying to get a domain name and TLD (without a subdomain) from a string of a user's URL, which may or may not have a protocol, directories, subdomains, file names, etc.
In other words, given any of the following:
example.com www.example.com sub.example.com example.com/whatever/hey.html http://example.com https://subdomain.example.com ftp://example.com/whatever/hey.html
I always need: example.com
.
Now this is what I am doing:
$hostParts = explode('.', parse_url($URL, PHP_URL_HOST)); $tld = array_pop($hostParts); $domain = array_pop($hostParts); $domain = $domain . "." . $tld;
However, if the URL is provided without a protocol, it is interrupted. Why parse_url
cannot get the host in this situation?
source share