获取顶级域名函数

  function parseHost($httpurl)
  {
    $httpurl = strtolower( trim($httpurl) );
    if(empty($httpurl)) return ;
    $regx1 = '/https?:\/\/(([^\/\?#]+\.)?([^\/\?#-\.]+\.)(com\.cn|org\.cn|net\.cn|com\.jp|co\.jp|com\.kr|com\.tw)(\:[0-9]+)?)/i';
    $regx2 = '/https?:\/\/(([^\/\?#]+\.)?([^\/\?#-\.]+\.)(cn|com|org|net|cc|biz|hk|jp|kr|name|me|tw|la)(\:[0-9]+)?)/i';
    $host = $tophost = '';
    if(preg_match($regx1,$httpurl,$matches))
    {
      $host = $matches[1];
    } elseif(preg_match($regx2, $httpurl, $matches)) {
      $host = $matches[1];
    }
    if($matches) $tophost = $matches[2] == 'www.' ? $host:$matches[3].$matches[4].$matches[5];
    return array($host,$tophost);
  }

 

posted on 2015-03-02 11:52  一条大鱼  阅读(319)  评论(0编辑  收藏  举报