给定一个URL,我如何使用公共后缀列表(有效顶级域名列表,例如此列表)提取注册域名?
例如,考虑a.bg
是有效的公共后缀:
http://www.test.start.a.bg/hello.html -> start.a.bg
http://test.start.a.bg/ -> start.a.bg
http://test.start.abc.bg/ -> abc.bg (.bg is the public suffix)
这不能使用简单的字符串操作来完成,因为公共后缀可以由多个级别组成,具体取决于TLD。
注:我如何读取列表(数据库或平面文件)并不重要,但列表应该可以在本地访问,所以我不总是依赖于外部服务。
您可以使用parse_url()
提取主机名,然后使用regdom提供的库确定已注册的域名(dn + eTLD)。例如:
require_once("effectiveTLDs.inc.php");
require_once("regDomain.inc.php");
$url = 'http://www.metu.edu.tr/dhasjkdas/sadsdds/sdda/sdads.html';
echo getRegisteredDomain(parse_url($url, PHP_URL_HOST));
将打印出metu.edu.tr
。
我尝试过的其他例子:
http://www.xyz.start.bg/hello -> start.bg
http://www.start.a.bg/world -> start.a.bg (a.bg is a listed eTLD)
http://xyz.ma219.metu.edu.tr -> metu.edu.tr
http://www.google.com/search -> google.com
http://google.co.uk/search?asd -> google.co.uk
更新:这些库已移动到:https://github.com/leth/registered-domains-php
这个问题有点老了,但是有一个新的解决方案:https://github.com/jeremykendall/php-domain-parser
这个库完全符合您的要求。下面是设置:
$pslManager = new PdpPublicSuffixListManager();
$parser = new PdpParser($pslManager->getList());
echo $parser->getRegisterableDomain('www.scottwills.co.uk');
这将打印"scottwills.co.uk"
我推荐使用tlextract,它有从PSL生成的可定期更新的数据库。
$extract = new LayerShifterTLDExtractExtract();
$result = $extract->parse('shop.github.com');
$result->getFullHost(); // will return (string) 'shop.github.com'
$result->getRegistrableDomain(); // will return (string) 'github.com'
$result->isValidDomain(); // will return (bool) true
$result->isIp(); // will return (bool) false