获取顶级域名函数

  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);
  }
原文地址:https://www.cnblogs.com/yutaoyao/p/4308408.html