Running parse_url() on a string that may not contain the protocol

谁说我不能喝 提交于 2019-12-31 03:33:26

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/19597849/running-parse-url-on-a-string-that-may-not-contain-the-protocol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!