使用PHP获取顶级域名信息

在做 酷瓜云课堂 系统授权相关功能的时候,需要针对顶级域名授权,所以就写了个函数,目前只是针对国内的复合域名做了处理, 其它国家的请自行扩充,简单够用就好。

/**
 * 获取顶级域名
 *
 * @param string $host
 * @return string
 */
function kg_top_domain($host)
{
    $data = explode('.', $host);

    $count = count($data);

    if (preg_match('/\.(com|net|org|gov|edu)\.cn$/', $host)) {
        $domain = $data[$count - 3] . '.' . $data[$count - 2] . '.' . $data[$count - 1];
    } else {
        $domain = $data[$count - 2] . '.' . $data[$count - 1];
    }

    return $domain;
}

你可能感兴趣的:(php)