给定URL,如何使用
Public Suffix List(有效TLD列表,例如
this list)提取注册域?
例如,考虑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()提取主机名,然后使用
library provided by 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