function quyuming($url) { //提取 主域名
$urlWithProtocol = 'http://' . $url;
$host = parse_url($urlWithProtocol, PHP_URL_HOST);
if ($host === null) {
return false;
}
$host = rtrim($host, '/');
$data = explode('.', $host);
$n = count($data);
$doubleSuffixPattern = '/[w].+.(com|net|org|gov|edu).cn$/';
if (($n > 2) && preg_match($doubleSuffixPattern, $host)) {
$tld = $data[$n - 3] . '.' . $data[$n - 2] . '.' . $data[$n - 1];
}
else {
$countryCodePattern = '/^(com|net|org|gov|edu|cc|[a-z]{2})$/';
$isCountryCode = preg_match($countryCodePattern, $data[$n - 2]);
if ($isCountryCode) {
$tld = $data[$n - 3] . '.' . $data[$n - 2] . '.' . $data[$n - 1];
} else {
$tld = $data[$n - 2] . '.' . $data[$n - 1];
}
}
return $tld;
}
网上很多这种函数,提取复杂的2级域名几乎全部都是错的,本站这套是纠正很多错误的版本