首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除域扩展

问删除域扩展
EN

Stack Overflow用户
提问于 2010-10-04 15:27:34
回答 5查看 9.6K关注 0票数 8

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

我尝试过使用str_replace,但不起作用,我猜只能使用正则表达式....

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-10-04 15:31:22

代码语言:javascript
复制
$subject = 'just-a.domain.com';
$result = preg_split('/(?=\.[^.]+$)/', $subject);

这将生成以下数组

代码语言:javascript
复制
$result[0] == 'just-a.domain';
$result[1] == '.com';
票数 10
EN

Stack Overflow用户

发布于 2010-10-04 15:49:45

代码语言:javascript
复制
  preg_match('/(.*?)((?:\.co)?.[a-z]{2,4})$/i', $domain, $matches);

$matches1将具有域名,而$matches2将具有扩展名

代码语言:javascript
复制
<?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);
}
?>

将产生输出

代码语言:javascript
复制
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
)
票数 12
EN

Stack Overflow用户

发布于 2010-10-04 15:51:36

如果您想要删除域名注册商管理的域名部分,您将需要使用类似于the Public Suffix List的后缀列表。

但是,由于遍历此列表并测试域名的后缀效率不高,因此只能使用此列表来构建索引,如下所示:

代码语言:javascript
复制
$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);

搜索最佳匹配的过程如下所示:

代码语言:javascript
复制
$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);

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

代码语言:javascript
复制
$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,
     ),
     // …
);

然后,您可以使用以下内容查找匹配项:

代码语言:javascript
复制
$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);
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3853338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档