<?php
/**
* @Author: Ding Jianlong
* @Date: 2019-03-07 16:14:04
* @Last Modified by: Ding Jianlong
* @Last Modified time: 2019-03-20 13:45:12
*/
header('content-type:text/html;charset=utf-8');
//获取顶级域名
function getTopHost($url){
$url = strtolower($url); //首先转成小写
$hosts = parse_url($url);
$host = $hosts['host'];
//查看是几级域名
$data = explode('.', $host);
$n = count($data);
//判断是否是双后缀
$preg = '/[\w].+\.(com|net|org|gov|edu)\.cn$/';
if(($n > 2) && preg_match($preg,$host)){
//双后缀取后3位
$host = $data[$n-3].'.'.$data[$n-2].'.'.$data[$n-1];
}else{
//非双后缀取后两位
$host = $data[$n-2].'.'.$data[$n-1];
}
return $host;
}
// 测试
echo getTopHost("http://ABC.com/s/j?wd=djl"),'<br>';
echo getTopHost("http://www.abc.com/s/j?wd=djl"),'<br>';
echo getTopHost("http://2.www.abc.com/s/j?wd=djl"),'<br>';
echo getTopHost("https://mp.weixin.qq.com/s?__biz=MzA3ODI3ODUzMw=="),'<br>';
echo getTopHost("http://cfi.net.cn/"),'<br>';
echo getTopHost("http://www.cfi.NEt.cn/"),'<br>';
echo getTopHost("https://www.sina.com.cn/?from=kandian"),'<br>';
<?php
/**
* @Author: Ding Jianlong
* @Date: 2019-03-07 16:14:04
* @Last Modified by: Ding Jianlong
* @Last Modified time: 2019-03-20 13:45:12
*/
header('content-type:text/html;charset=utf-8');
//获取顶级域名
function getTopHost($url){
$url = strtolower($url); //首先转成小写
$hosts = parse_url($url);
$host = $hosts['host'];
//查看是几级域名
$data = explode('.', $host);
$n = count($data);
//判断是否是双后缀
$preg = '/[\w].+\.(com|net|org|gov|edu)\.cn$/';
if(($n > 2) && preg_match($preg,$host)){
//双后缀取后3位
$host = $data[$n-3].'.'.$data[$n-2].'.'.$data[$n-1];
}else{
//非双后缀取后两位
$host = $data[$n-2].'.'.$data[$n-1];
}
return $host;
}
// 测试
echo getTopHost("http://ABC.com/s/j?wd=djl"),'<br>';
echo getTopHost("http://www.abc.com/s/j?wd=djl"),'<br>';
echo getTopHost("http://2.www.abc.com/s/j?wd=djl"),'<br>';
echo getTopHost("https://mp.weixin.qq.com/s?__biz=MzA3ODI3ODUzMw=="),'<br>';
echo getTopHost("http://cfi.net.cn/"),'<br>';
echo getTopHost("http://www.cfi.NEt.cn/"),'<br>';
echo getTopHost("https://www.sina.com.cn/?from=kandian"),'<br>';
运行结果:
abc.com
abc.com
abc.com
qq.com
cfi.net.cn
cfi.net.cn
sina.com.cn
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询