2010-10-04 136 views
6

所以我们假设我有just-a.domain.com,just-a-domain.info,just.a-domain.net如何删除扩展名.com,.net.info ...,我需要两个变量,一个域名和另一个扩展名。删除域名扩展

我试着用str_replace但不工作,我想只能用正则表达式....

+5

哪些部分应该www.google.co.uk回报? – Matthew 2010-10-04 07:28:52

回答

8
$subject = 'just-a.domain.com'; 
$result = preg_split('/(?=\.[^.]+$)/', $subject); 

这将产生以下阵列

$result[0] == 'just-a.domain'; 
$result[1] == '.com'; 
+0

的作品,但只适用于扩展名中带有一个DOT的域。例如。对于“.com.br”它不起作用。 – almo 2014-09-04 19:51:53

+0

@almo使用脚本获得结果,然后再分割结果:) – 2015-07-28 06:33:55

-1
strrpos($str, ".") 

会给你上期指数在你的字符串,那么你可以使用substr()与索引并返回短字符串。

+0

它不适用于.co.uk .com.br等 – 2015-05-31 08:55:45

10
preg_match('/(.*?)((?:\.co)?.[a-z]{2,4})$/i', $domain, $matches); 

$匹配[1]将具有域和$比赛[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 
) 
7

如果您想删除由域名注册商管理的域名部分,您需要使用类似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); 
0

正则表达式和parse_url()是不适合你的解决方案。

您需要使用Public Suffix List的软件包,只有这样您才能正确提取具有二级,三级TLD(co.uk,a.bg,b.bg等)的域。我建议使用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'