给定一个 URL,我如何使用 Public Suffix List 提取注册域(有效 TLD 列表,例如 this list )?
例如,考虑 a.bg
是一个有效的公共(public)后缀:
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)
这不能使用简单的字符串操作来完成,因为公共(public)后缀可以包含多个级别,具体取决于 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
关于php - 根据公共(public)后缀列表从 URL 中提取注册域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8272805/