代码之家  ›  专栏  ›  技术社区  ›  Uffo

删除域扩展

  •  6
  • Uffo  · 技术社区  · 14 年前

    假设我有 just-a.domain.com,just-a-domain.info,just.a-domain.net 如何删除扩展 .com,.net.info ... 我需要两个变量的结果,一个是域名,另一个是扩展名。

    str_replace 但不起作用,我猜只有正则表达式。。。。

    5 回复  |  直到 12 年前
        1
  •  10
  •   HamZa    11 年前
    $subject = 'just-a.domain.com';
    $result = preg_split('/(?=\.[^.]+$)/', $subject);
    

    这将生成以下数组

    $result[0] == 'just-a.domain';
    $result[1] == '.com';
    
        2
  •  12
  •   Joyce Babu    9 年前
      preg_match('/(.*?)((?:\.co)?.[a-z]{2,4})$/i', $domain, $matches);
    

    $matches[1]将拥有域,$matches[2]将拥有扩展名

    <?php
    
    $domains = array("google.com", "google.in", "google.co.in", "google.info", "analytics.google.com");
    
    foreach($domains as $domain){
      preg_match('/(.*?)((?:\.co)?.[a-z]{2,4})$/i', $domain, $matches);
      print_r($matches);
    }
    ?>
    

    Array
    (
        [0] => google.com
        [1] => google
        [2] => .com
    )
    Array
    (
        [0] => google.in
        [1] => google
        [2] => .in
    )
    Array
    (
        [0] => google.co.in
        [1] => google
        [2] => .co.in
    )
    Array
    (
        [0] => google.info
        [1] => google
        [2] => .info
    )
    Array
    (
        [0] => analytics.google.com
        [1] => analytics.google
        [2] => .com
    )
    
        3
  •  8
  •   Gumbo    14 年前

    如果您想删除由域名注册者管理的域的一部分,您将需要使用这样的后缀列表 the Public Suffix List .

    $tlds = array(
        // ac : http://en.wikipedia.org/wiki/.ac
        'ac',
        'com.ac',
        'edu.ac',
        'gov.ac',
        'net.ac',
        'mil.ac',
        'org.ac',
        // ad : http://en.wikipedia.org/wiki/.ad
        'ad',
        'nom.ad',
        // …
    );
    $tldIndex = array_flip($tlds);
    

    然后搜索最佳匹配将如下所示:

    $levels = explode('.', $domain);
    for ($length=1, $n=count($levels); $length<=$n; ++$length) {
        $suffix = implode('.', array_slice($levels, -$length));
        if (!isset($tldIndex[$suffix])) {
            $length--;
            break;
        }
    }
    $suffix = implode('.', array_slice($levels, -$length));
    $prefix = substr($domain, 0, -strlen($suffix) - 1);
    

    或者构建一个表示域名级别层次结构的树,如下所示:

    $tldTree = array(
        // ac : http://en.wikipedia.org/wiki/.ac
        'ac' => array(
            'com' => true,
            'edu' => true,
            'gov' => true,
            'net' => true,
            'mil' => true,
            'org' => true,
         ),
         // ad : http://en.wikipedia.org/wiki/.ad
         'ad' => array(
            'nom' => true,
         ),
         // …
    );
    

    然后您可以使用以下方法查找匹配项:

    $levels = explode('.', $domain);
    $r = &$tldTree;
    $length = 0;
    foreach (array_reverse($levels) as $level) {
        if (isset($r[$level])) {
            $r = &$r[$level];
            $length++;
        } else {
            break;
        }
    }
    $suffix = implode('.', array_slice($levels, - $length));
    $prefix = substr($domain, 0, -strlen($suffix) - 1);
    
        4
  •  2
  •   Hassaan    8 年前

    parse_url() 不是你的解决方案。

    你需要一个 Public Suffix List TLD Extract .

    $extract = new LayerShifter\TLDExtract\Extract();
    
    $result = $extract->parse('just.a-domain.net');
    $result->getSubdomain(); // will return (string) 'just'
    $result->getHostname(); // will return (string) 'a-domain'
    $result->getSuffix(); // will return (string) 'net'
    $result->getRegistrableDomain(); // will return (string) 'a-domain.net'
    
        5
  •  -1
  •   Ólafur Waage    14 年前
    strrpos($str, ".")
    

    将为您提供字符串中最后一个句点的索引,然后您可以使用 substr()