前2次写的其实不太好,使用起来不是很方便。今天突然发现一个问题,如果当域名为www.***.com时候,他的顶级域名应该是***.com,而不是www.****.com。当时的写法故意把顶级域名设定为了www.***.com。后来带来了很多不方便。更正后的代码:
注意:代码高亮highlight的时候第7行empty那里总是多显示一个empty,可以直接点击查看源码。
if(!function_exists("parseHost"))
{
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|info|us|fr|de|tv|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[3].$matches[4];
$domainLevel = $matches[2] == 'www.' ? 1:(substr_count($matches[2],'.')+1);
} else {
$tophost = '';
$domainLevel = 0;
}
return array($domainLevel,$tophost,$host);
}
}
测试代码:
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|info|us|fr|de|tv|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[3].$matches[4];
$domainLevel = $matches[2] == 'www.' ? 1:(substr_count($matches[2],'.')+1);
} else {
$tophost = '';
$domainLevel = 0;
}
print_r($matches);
return array($domainLevel,$tophost,$host);
}
$url = 'http://3.register.login.www.***.com/';
$arr = parseHost($url);
print_r($arr);
/*
X-Powered-By: PHP/5.2.0
Content-type: text/html
Array
(
[0] => http://3.register.login.www.***.com/
[1] => 3.register.login.www.***.com
[2] => 3.register.login.www.
[3] => 35dalu.
[4] => com
)
Array
(
[0] => 5
[1] => 35dalu.com
[2] => 3.register.login.www.***.com
)
*/