如何使用PHP获取URL的子域?

时间:2015-12-13 01:10:09

标签: php parsing url subdomain

我有一些这样的网址:

1.  https://www.example.com/classname/method/arg      // {nothing}
2.  http://www.example.com/classname/method/arg       // {nothing}
3.  https://example.com/classname/method/arg          // {nothing}
4.  http://example.com/classname/method/arg           // {nothing}
5.  www.example.com/classname/method/arg              // {nothing}
6.  example.com/classname/method/arg                  // {nothing}
7.  sub.example.com/classname/method/arg              // sub
8.  www.sub.example.com/classname/method/arg          // sub
9.  http://sub.example.com/classname/method/arg       // sub
10. https://sub.example.com/classname/method/arg      // sub
11. http://www.sub.example.com/classname/method/arg   // sub
12. https://www.sub.example.com/classname/method/arg  // sub

// $url ^                                             // What I want ^

现在,正如您所见,我希望获得这些网址的sobdomain。怎么样?

我有两种方法,但它们都不适用于所有网址:

首先: (这只适用于7

echo array_shift((explode(".",$url)));

第二: (稍微好一点)

$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['host']);
echo $host[0];

3 个答案:

答案 0 :(得分:1)

使用parse_url

$url = 'http://sub.example.com/classname/method/arg';

$parsedUrl = parse_url($url);

$host = explode('.', $parsedUrl['host']);

$subdomain = $host[0];
echo $subdomain;

对于多个子域,您应该这样做

$url = 'http://en.sub.example.com/classname/method/arg';

$parsedUrl = parse_url($url);

$host = explode('.', $parsedUrl['host']);

$subdomains = array_slice($host, 0, count($host) - 2 );
print_r($subdomains);

答案 1 :(得分:0)

您使用explode()走在正确的轨道上,但您也应该使用parse_url()功能从网址see here for docs获取域名。 TL; DR:给它一个URL作为唯一的参数,然后返回一个单独分解的URL的所有部分的数组。

话虽如此,更大的问题是你如何区分subdomain.somesite.com和somesite.co.uk--第一个显然有一个子域,但第二个没有。我担心除了与顶级域名列表进行比较之外,我没有其他智能解决方案。

答案 2 :(得分:0)

我将在这里留下......

使用@TwoStraws的想法,我创建了一个函数,它将使用data.iana.org的最新TLD列表从给定的URL提供子域,基本域和TLD域部分。

function GetDomainParts($URL,$TLDs_List = 'http://data.iana.org/TLD/tlds-alpha-by-domain.txt') {
    // Get a list of all top level domains
    $TLDs = explode(PHP_EOL,file_get_contents($TLDs_List));
    unset($TLDs[0]); array_values($TLDs);

    // And since that list has all the country codes too, lets assume all 2 letter domains are country codes, and get that list too
    $CC_TLDs = [];
    foreach($TLDs as $TLD) {
        if(strlen($TLD) == 2) {
            $CC_TLDs[] = $TLD;
        }
    }

    // Now lets take our URL and remove some things
    $ParsedUrl = parse_url($URL);
    $Host = explode('.', $ParsedUrl['host']);

    // If we cant find it, we return false...
    $BaseDomain = false;
    $TLDDomain = false;

    // And look at the last 2 items in the Host array, these will be our TLD's (possibly)
    $N_Minus_1 = strtoupper(isset($Host[count($Host)-1])?$Host[count($Host)-1]:null);
    $N_Minus_2 = strtoupper(isset($Host[count($Host)-2])?$Host[count($Host)-2]:null);

    // This has a potential of being our base domain, but may not be there
    $N_Minus_3 = strtoupper(isset($Host[count($Host)-3])?$Host[count($Host)-3]:null);


    // We first check our N Minus 1 against our list of Country Code TLDs
    if(in_array($N_Minus_1,$CC_TLDs)) {
        // If N Minus 1 is in the Country Code, We can check our N Minus 2 and see if it is in the TLDs array
        if(in_array($N_Minus_2,$TLDs)) {
            // If N Minus 2 is in the list of TLDs, we make the assumption that this is part of the TLD, making N Minus 3 our Base Domain
            $BaseDomain = $N_Minus_3;
            $TLDDomain = $N_Minus_2.'.'.$N_Minus_1;

            // We unset the parts that are used, the rest is our sub domain
            unset($Host[count($Host)-1]);
            unset($Host[count($Host)-1]);
            unset($Host[count($Host)-1]);
            $SubDomain = implode('.',$Host);
        } else {
            // If N Minus 2 is NOT in the list of TLDs, we make the assumption that this is our Base Domain
            $BaseDomain = $N_Minus_2;
            $TLDDomain = $N_Minus_1;

            // We unset the parts that are used, the rest is our sub domain
            unset($Host[count($Host)-1]);
            unset($Host[count($Host)-1]);
            $SubDomain = implode('.',$Host);
        }
    } else {
        // If N Minus 1 is NOT in the Country Codes, we can assume it is the TLD, lets check it against the TLDs to make sure
        if(in_array($N_Minus_1,$TLDs)) {
            // If N Minus 1 Is in our List of TLDs, we can assume we found our TLD, so N Minus 2 must be our Base Domain
            $BaseDomain = $N_Minus_2;
            $TLDDomain = $N_Minus_1;

            // We unset the parts that are used, the rest is our sub domain
            unset($Host[count($Host)-1]);
            unset($Host[count($Host)-1]);
            $SubDomain = implode('.',$Host);
        } else {
            // If N Minus 1 is NOT in our list of TLDs it is either a new TLD unheard of by iana.org, or does not exist, lets make the assumption that it is the tld
            $BaseDomain = $N_Minus_2;
            $TLDDomain = $N_Minus_1;

            // We unset the parts that are used, the rest is our sub domain
            unset($Host[count($Host)-1]);
            unset($Host[count($Host)-1]);
            $SubDomain = implode('.',$Host);

            // Not sure if it is needed, but at this point we can swap the checks, checking minus 2 as the country code and minus 1 as the TLD, 
            // but I am not sure this is ever a real world scenerio, and am unable to find any proof to support this theory
        }

    }

    // Return our URL Parts ( DISCLAIMER: Note that this will not solve every URL, such as WWW.AFAMILYCOMPANY.CO, 
    // because both AFAMILYCOMPANY and CO are TLDs one being a TLD and the other being a Country Code, Leaving "WWW" as the Base Domain.
    // I use this functionality to auto-populate a user changeable setting, just in case my assumption is wrong the user can fix it.
    // One should not assume this will work 100% of the time! )
    return [strtolower($SubDomain),strtolower($BaseDomain),strtolower($TLDDomain)];
}

请阅读免责声明......

请注意,这不会解决每个URL,例如WWW.AFAMILYCOMPANY.CO,因为AFAMILYCOMPANY和CO都是顶级域名,一个是顶级域名,另一个是国家代码,留下“WWW”作为基础域。我使用此功能自动填充用户可更改的设置,以防万一我的假设是错误的,用户可以修复它。不应该假设这将在100%的时间内起作用!

进一步注意,http://whois.domaintools.com/afamilycompany.co被列为“受限制和保留名称”域。如果互联网正在做正确的事情,那么这些类型的域应该永远不会在生产中,因此这个功能是安全的。

检查此功能是否适用于您的域的一种简单方法是转到http://data.iana.org/TLD/tlds-alpha-by-domain.txt按Ctrl + F并检查域是否在列表中,如果是,则功能将失败,如果不是,该功能将100%的时间工作。我意识到这只是朝着正确方向迈出的一步,所以如果有其他人可以加入这个想法,请告诉我。