2015-09-17 33 views
2

我知道網上有很多關於這個主題的信息,但是我似乎無法想象我想要的方式。來自url的PHP Strip域名

我試圖建立從一個url條域名的功能:只需要在域的原名

http://blabla.com blabla 
www.blabla.net  blabla 
http://www.blabla.eu blabla 

隨着parse_url我得到了過濾域,但這還不夠。 我有3個功能是高新區域,但我仍然得到了一些錯誤的輸出

function prepare_array($domains) 
{ 
    $prep_domains = explode("\n", str_replace("\r", "", $domains)); 
    $domain_array = array_map('trim', $prep_domains); 

    return $domain_array; 
} 

function test($domain) 
{ 
    $domain = explode(".", $domain); 
    return $domain[1]; 
} 

function strip($url) 
{ 
    $url = trim($url); 
    $url = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url); 
    $url = preg_replace("/\/.*$/is" , "" ,$url); 
    return $url; 
} 

每一個可能的域,URL和擴展名是允許的。函數完成後,它必須返回一個只包含域名本身的數組。

更新: 感謝您的所有建議!

我在大家的幫助下計算出來的。

function test($url) 
{ 
    // Check if the url begins with http:// www. or both 
    // If so, replace it 
    if (preg_match("/^(http:\/\/|www.)/i", $url)) 
    { 
     $domain = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url); 
    } 
    else 
    { 
     $domain = $url; 
    } 

    // Now all thats left is the domain and the extension 
    // Only return the needed first part without the extension  
    $domain = explode(".", $domain); 

    return $domain[0]; 
} 
+0

嘗試使用'parse_url'功能來做到這一點。 http://php.net/manual/function.parse-url.php – ChoiZ

+0

子域名呢? –

回答

0
function test($url) 
{ 
    // Check if the url begins with http:// www. or both 
    // If so, replace it 
    if (preg_match("/^(http:\/\/|www.)/i", $url)) 
    { 
     $domain = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url); 
    } 
    else 
    { 
     $domain = $url; 
    } 

    // Now all thats left is the domain and the extension 
    // Only return the needed first part without the extension  
    $domain = explode(".", $domain); 

    return $domain[0]; 
} 
2

如何

$wsArray = explode(".",$domain); //Break it up into an array. 
$extension = array_pop($wsArray); //Get the Extension (last entry) 
$domain = array_pop($wsArray); // Get the domain 

http://php.net/manual/en/function.array-pop.php

+0

其實ChoiZ上面的評論可能是更好的答案。 –

+0

[這個回答在'.co.uk'和類似的域名上不起作用。](https://ideone.com/uyrYdv) – Zsw

+0

不幸的是,這並不能完成這項工作。當我輸入http:// google。它會返回http:// google – Rob

1

啊,你的問題在於一個事實,即頂級域名可以是一個或兩個部分e.g .COM VS .co.uk。

我會做的是維護一個TLD列表。通過parse_url之後的結果,查看列表並查找匹配項。去掉頂級域名,在''上爆炸。最後一部分將採用你想要的格式。

這看起來並不像它那樣高效,但是隨着TLD一直被添加,我看不到任何其他確定性方式。

0

試用preg_replace。

類似於 $ domain = preg_replace($ regex,'$ 1',$ url);

regex

+0

這並不回答問題,因爲鏈接中提供的正則表達式沒有任何捕獲組。 – Zsw

1

好吧......這是凌亂的,你應該花一些時間來優化和緩存以前的結構域。你還應該有一個友好的名稱服務器,最後一個問題是域名必須在DNS中有一個「A」記錄。

這將嘗試按相反順序組裝域名,直到它可以解析爲DNS「A」記錄。

在anyrate,這是竊聽我,所以我希望這個答案可以幫助:

<?php 
$wsHostNames = array(
    "test.com", 
    "http://www.bbc.com/news/uk-34276525", 
    "google.uk.co" 
); 
foreach ($wsHostNames as $hostName) { 
    echo "checking $hostName" . PHP_EOL; 
    $wsWork = $hostName; 
    //attempt to strip out full paths to just host 
    $wsWork = parse_url($hostName, PHP_URL_HOST); 
    if ($wsWork != "") { 
     echo "Was able to cleanup $wsWork" . PHP_EOL; 
     $hostName = $wsWork; 
    } else { 
     //Probably had no path info or malformed URL 
     //Try to check it anyway 
     echo "No path to strip from $hostName" . PHP_EOL; 
    } 

    $wsArray = explode(".", $hostName); //Break it up into an array. 

    $wsHostName = ""; 
    //Build domain one segment a time probably 
    //Code should be modified not to check for the first segment (.com) 
    while (!empty($wsArray)) { 
     $newSegment = array_pop($wsArray); 
     $wsHostName = $newSegment . $wsHostName; 
     echo "Checking $wsHostName" . PHP_EOL; 
     if (checkdnsrr($wsHostName, "A")) { 
      echo "host found $wsHostName" . PHP_EOL; 
      echo "Domain is $newSegment" . PHP_EOL; 
      continue(2); 
     } else { 
      //This segment didn't resolve - keep building 
      echo "No Valid A Record for $wsHostName" . PHP_EOL; 
      $wsHostName = "." . $wsHostName; 
     } 
    } 
    //if you get to here in the loop it could not resolve the host name 

} 
?>