问题描述
我正在尝试从用户输入的URL字符串中获取域名和TLD(无子域),该字符串可能具有也可能没有协议,目录,子域,文件名等.
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
我应该总是以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;
但是,如果提供的URL没有协议,则会中断.为什么parse_url
在这种情况下无法获取主机?
However, if a URL is provided without the protocol, it breaks. Why is parse_url
failing to get the host in this situation?
推荐答案
根据定义,URL包含协议或方案.检查////,如果不存在,则//放在字符串的前面.这在PHP<中可能有所不同. 5.4.7,如果没有协议,则可以添加http://.
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.
这篇关于在可能不包含协议的字符串上运行parse_url()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!