PHP 解析 URL 中的域名 [英] Parsing Domainname From URL In PHP

查看:41
本文介绍了PHP 解析 URL 中的域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 PHP 中的 URL 解析域?看来我需要一个国家域数据库.

How I can parse a domain from URL in PHP? It seems that I need a country domain database.

示例:

http://mail.google.com/hfjdhfjd/jhfjd.html-> google.com
http://www.google.bg/jhdjhf/djfhj.html -> 谷歌.bg
http://www.google.co.uk/djhdjhf.php -> 谷歌.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

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 请求来完成吗?

Can it be done with whois request?

.tr (www.nic.tr, www.tsk.tr) 的域名很少,其他的都是如您所知:www.something.com.trwww.something.org.tr

There are few domain names with .tr (www.nic.tr, www.tsk.tr) the others are as you know: www.something.com.tr, www.something.org.tr

也没有www.something.com.bgwww.something.org.bg.他们www.something.bg就像德国人的.de

Also there is no www.something.com.bg, www.something.org.bg. They are www.something.bg like the Germans' .de

但是有 www.something.a.bg, www.something.b.bg 因此 a.bg, b.bgc.bg 等等.(a.bg 就像 co.uk)

But there are www.something.a.bg, www.something.b.bg thus a.bg, b.bg, c.bg and so on. (a.bg is like co.uk)

网上一定有这些顶级域名的列表.

There on the net must be list of these top domain names.

检查 url http://www.agrotehnika97.a.bg/ 在 Internet Explorer 中的颜色.也检查

Check how is coloured the url http://www.agrotehnika97.a.bg/ in Internet Explorer. Check also

www.google.co.uk<br>
www.google.com.tr<br>
www.nic.tr<br>
www.tsk.tr

推荐答案

域存储在 $_SERVER['HTTP_HOST'] 中.

我相信这会返回整个域.要获取顶级域,您可以这样做:

I believe this returns the whole domain. To just get the top-level domain, you could do this:

// 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;

这篇关于PHP 解析 URL 中的域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆