解析URL中的域名在PHP中
如何在PHP中解析URL中的域名?看来我需要一个国家/地区域数据库。解析URL中的域名在PHP中
实例:
http://mail.google.com/hfjdhfjd/jhfjd.html - > google.com
http://www.google.bg/jhdjhf/djfhj.html - > google.bg
http://www.google.co.uk/djhdjhf.php - >使用google.co.uk
http://www.tsk.tr/jhjgc.aspx - > tsk.tr
http://subsub.sub.nic.tr/ - > NIC .tr
http://subsub.sub.google.com.tr - > google.com.tr
http://subsub.sub.itoy.info.tr - > itoy.info.tr
可以使用whois请求完成吗?
编辑:有几个域名与.tr
(www.nic.tr
,www.tsk.tr
)其他都是如你所知:www.something.com.tr
,www.something.org.tr
还没有www.something.com.bg
,www.something.org.bg
。他们www.something.bg
像德国.de
但也有www.something.a.bg
,www.something.b.bg
从而a.bg
,b.bg
,c.bg
等。 (a.bg
就像co.uk
)
在网络上有必须列出这些顶级域名。
检查如何在Internet Explorer中对url http://www.agrotehnika97.a.bg/
着色。 检查也
www.google.co.uk<br>
www.google.com.tr<br>
www.nic.tr<br>
www.tsk.tr
域被存储在$_SERVER['HTTP_HOST']
。
编辑:我相信这会返回整个域。刚刚获得顶级域名,你可以这样做:
// Add all your wanted subdomains that act as top-level domains, here (e.g. 'co.cc' or 'co.uk')
// As array key, use the last part ('cc' and 'uk' in the above examples) and the first part as sub-array elements for that key
$allowed_subdomains = array(
'cc' => array(
'co'
),
'uk' => array(
'co'
)
);
$domain = $_SERVER['HTTP_HOST'];
$parts = explode('.', $domain);
$top_level = array_pop($parts);
// Take care of allowed subdomains
if (isset($allowed_subdomains[$top_level]))
{
if (in_array(end($parts), $allowed_subdomains[$top_level]))
$top_level = array_pop($parts).'.'.$top_level;
}
$top_level = array_pop($parts).'.'.$top_level;
这不完全是ilhan之后的事情。 – 2010-02-24 17:00:14
为什么不呢?编辑修复了它。 – Franz 2010-02-24 17:01:42
即使在编辑之后它仍然不起作用;-)。它不涉及google.co.uk案件,因为这会返回'co.uk'。 – 2010-02-24 17:06:30
您可以使用parse_url()
将其分割,并得到你想要的。 下面是一个例子...
$url = 'http://www.google.com/search?hl=en&source=hp&q=google&btnG=Google+Search&meta=lr%3D&aq=&oq=dasd'; print_r(parse_url($url));
回音必...
Array ( [scheme] => http [host] => www.google.com [path] => /search [query] => hl=en&source=hp&q=google&btnG=Google+Search&meta=lr%3D&aq=&oq=dasd )
我在开始时做了同样的错误。不过,他只想要google.com。 – Franz 2010-02-24 17:02:09
我明白了。够公平 - 他可以'preg_match()'来获得其余的。假设'$ url_split'是解析的URL - 这可以通过... preg_match('/ www \。?([\ w \ - \。] +)([az \。] +)/ i' ,$ url_split ['host'],$ matches)' - 然后他可以使用'$ matches [1]。$ matches [2]'来获取没有第一个域的主机。问题在于,你永远无法预测子域会走多远 - 它可能是“sub1.sub2.domain.co.uk” - 这会获取“sub2.domain.co.uk”,而不是域。 co.uk' – casraf 2010-02-24 17:11:54
我想你需要一个域名后使用的所有后缀的列表。 http://publicsuffix.org/list/提供当前使用的所有后缀的最新(或声明)。 名单实际上是here 现在的想法是让你这个列表解析成一个结构,不同层次由点分裂,年底的水平开始回升:
所以例如用于域: COM .la com.tr com。LC
你最终用:
[la]=>[com]
[lc]=>[com]
等等
然后你会得到从BASE_URL主机(通过使用parse_url),而且你用点爆发, 。你开始匹配你的结构的价值观,从最后一个开始:
所以对于google.com.tr你会开始匹配tr,然后com,那么你一旦得到匹配就不会找到匹配谷歌,这是你想要的...
正则表达式和parse_url()不是你的解决方案。
您需要使用Public Suffix List的包,只有这样您才能正确提取具有两级,三级TLD(co.uk,a.bg,b.bg等)的域。我建议使用TLD Extract。代码
这里例如:
$extract = new LayerShifter\TLDExtract\Extract();
$result = $extract->parse('http://subsub.sub.google.com.tr');
$result->getRegistrableDomain(); // will return (string) 'google.com.tr'
注,即* co.uk *,* com.tr *和* info.tr *本身是完全有效的域/主机名,而所有这些都是*不*顶级域名。正如'google.co.uk'中的'google'只是* co.uk'的一个子域。鉴于你可以自由组合几乎所有的东西,你可能无法为此做出完整的表格。 – poke 2010-02-24 17:19:36
@poke,我在网站上看到了这个列表。 Firefox正在使用该网站的列表。但我不记得它。 – ilhan 2010-02-24 17:27:41
http://publicsuffix.org – Franz 2010-02-24 17:44:06