只获取域名,不包括.com www和子域名

时间:2012-08-23 05:23:00

标签: php

我必须从我提供的某些网站列表中获取域名。我必须只选择除.com,www和子域名之外的商店名称。同样,我只需要一个商店名称并插入db

例如。列表是

shopping.indiatimes.com
shopping.indiatimes.com
shopping.indiatimes.com
www.infibeam.com
adexmart.com
deals.sulekha.com    
deals.sulekha.com
www.ebay.in
deals.ebay.in
shop.seventymm.com
www.homeshop18.com
www.buytheprice.com
www.snapdeal.com
www.aircel.com
www.tradus.com
www.infibeam.com
www.futurebazaar.com
www.tradus.com
www.shoppingmantra.com
camera.zoomin.com
www.homeshop18.com

所以在这里我只想要域名,例如indiatimes,ebay,tradus,zoomin..etc。

4 个答案:

答案 0 :(得分:1)

foreach($domains as $domain)
   echo implode('', array_slice(explode('.', $domain), -2, 1));

答案 1 :(得分:0)

这可以通过执行字符串爆炸然后获取倒数第二个数组元素来完成。

$array = explode(".", "www.snapdeal.com");

$count = count($array);
$value = $array[$count - 2]

答案 2 :(得分:0)

$url_to_check = "http://www.shopping.indiatimes.com";
$url1 = preg_replace('#^https?://#', '', $url_to_check);
$domain = implode('', array_slice(explode('.', $url1), -2, 1));
echo $domain; //this will echo indiatimes

答案 3 :(得分:0)

$domain = parse_url("www.domainnames.com");
    $host = $domain['host'];       
     $exp  = explode('.' , $host);        
                if(isset($exp[2])){        
                $domainname = ($exp[1]);
                }else {           
                    $domainname = ($exp[0]);
                }
                $a []= $domainname;  

      $uniquenames = (array_unique($a));    
      print_r($uniquenames);