根据公共后缀列表从 URL 中提取注册域

2023-12-30

给定一个 URL,如何使用以下命令提取注册域公共后缀列表 http://publicsuffix.org/list/(有效 TLD 列表,例如这个清单 http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1)?

例如,考虑到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 提供的库 http://www.dkim-reputation.org/regdom-lib-downloads/确定注册域名(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/leth/registered-domains-php

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据公共后缀列表从 URL 中提取注册域 的相关文章

随机推荐