2010-10-04 173 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'