By definition a URL contains a protocol or scheme. Check for // and if not present prepend // to the string. This may be different in PHP < 5.4.7 so maybe add http:// if no protocol.
Running parse_url() on a string that may not contain the protocol
Pregunta
I'm trying to get the domain name and TLD (no subdomain) from a user-input URL string which may or may not have protocol, directories, subdomains, filenames, 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 should always end up with: example.com
.
Right 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 a URL is provided without the protocol, it breaks. Why is parse_url
failing to get the host in this situation?
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow