记录一些常用的函数和瞎折腾的过程……

PHP获取顶级域名函数

废话不多说,直接上函数,这个函数也是网上整理的,很实用的,有需要的可以参考:

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);
	}
} 

测试:

print_r(parseHost('https://www.psay.cn/toss/106.html'));

结果:

Array
(
    [0] => 1
    [1] => psay.cn
    [2] => www.psay.cn
)
《PHP获取顶级域名函数》没有评论
发表评论