2010-08-13 75 views
0

我有一個網址列表,如下面列在我的數據庫中。從網站地址列表中提取頂級域名

我需要從列表中的每個地址獲取域名。

  • http://en.wordpress.com/tag/1000-things-we-hate/
  • http://en.wordpress.com/tag/1019/
  • http://en.wordpress.com/tag/1030-am/
  • http://www.yahoo.com/index.html
  • http://www.msn.com/index.html

回答

1

這裏有一個辦法做到這一點在Java中:。

String input = "http://en.wordpress.com/tag/1000-things-we-hate/"; 
// Assuming that all urls start with "http://" 
int finish = input.indexOf("/", 7); 
if(finish == -1) 
{ 
    finish = input.length(); 
} 
System.out.println(input.substring(7, finish)); 

打印en.wordpress.com(?我以爲是你想要的)

0
<?php 
$url = "http://en.wordpress.com/tag/1000-things-we-hate/"; 
$bits = explode("/",$url); 
$nextBits = explode(".",$bits[1]); 
$count = count($nextBits); 
$domain = $nextBits[$count-1].".".$nextBits[$count]; 
echo $domain; 
?> 
+2

有在代碼中的錯誤按預期它不工作 既使它會工作打算,它WOU ld不夠。 「http://bbc.co.uk/」將返回「co.uk」 – Sam 2010-08-13 22:13:12

+0

啊......是的......沒有想到這一點。好點。 – 2010-08-14 17:01:06

+0

我找到了2個頂級域名集合https://docs.google.com/file/d/0B8ALaar6dLM7ZUc2MUtidVE4RXM/edit?usp=sharing 和 http://mxr.mozilla.org/mozilla-central/source/ netwerk/DNS/effective_tld_names.dat?原料= 1 – Romeno 2013-03-29 15:10:59

0
<?php 
echo parse_url($url, PHP_URL_HOST); 

這將返回「en.wordpress.com」。如果你不希望子域(即只「wordpress.com),那麼事情越來越複雜了。你可能會需要像http://www.dkim-reputation.org/regdom-libs/