10

给定一个 URL,我如何使用公共后缀列表(有效 TLD 列表,例如此列表)提取注册域?

例如,考虑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。

PS我如何读取列表(数据库或平面文件)并不重要,但列表应该可以在本地访问,所以我并不总是依赖外部服务。

4

3 回答 3

17

您可以使用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

于 2011-11-25T19:38:06.210 回答
4

这个问题有点老了,但是有一个新的解决方案:https ://github.com/jeremykendall/php-domain-parser

这个库正是你想要的。这是设置:

$pslManager = new Pdp\PublicSuffixListManager();
$parser = new Pdp\Parser($pslManager->getList());
echo $parser->getRegisterableDomain('www.scottwills.co.uk');

这将打印"scottwills.co.uk".

于 2015-04-07T19:48:41.687 回答
1

我建议使用TLDExtract ,它具有从PSL生成的可定期更新的数据库。

$extract = new LayerShifter\TLDExtract\Extract();

$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
于 2016-06-20T10:27:57.483 回答