php - 根据公共(public)后缀列表从 URL 中提取注册域

标签 php url domain-name

给定一个 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

更新:这些库已移至:https://github.com/leth/registered-domains-php

关于php - 根据公共(public)后缀列表从 URL 中提取注册域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8272805/

相关文章:

php - 下面的 driver_options 参数是什么意思?

php - 在您的实时站点上隐藏显示 mysql 查询是否安全

javascript - 使用 Javascript 解析 URL

google-app-engine - 一个谷歌应用引擎应用程序如何服务于多个域?

javascript - Laravel 5 依赖下拉列表方法

php - mysql_data_seek 不工作

JavaScript 的 URL 绝对路径在一个页面上有效,但在另一个页面上创建 ajax 错误消息

javascript - 通过 SRC 属性将变量传递给 JavaScript

domain-name - WWW 或非 WWW,选择什么作为主站点名称?

java - 创建自定义 *.domain.com 域用于注册