知识屋:更实用的电脑技术知识网站
所在位置:首页 > 编程技术  > PHP编程

由字符串,提取完整子域名的方法 -php

发表时间:2014-09-05来源:网络

具体代码如下:

    function get_domain($url){    $pattern = "/[w-] .(com|net|org|gov|cc|biz|info|cn)(.(cn|hk))*/";    preg_match($pattern, $url, $matches);    if(count($matches) > 0) {    return $matches[0];    }else{    $rs = parse_url($url);	    $main_url = $rs["host"];    if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {    return $main_url;    }else{    $arr = explode(".",$main_url);    $count=count($arr);    $endArr = array("com","net","org","3322");//com.cn net.cn 等情况    if (in_array($arr[$count-2],$endArr)){		if(isset($arr[$count-4])){			$domain = "http://".$arr[$count-4].".".$arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];		}else{			$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];		}    }else{		if(isset($arr[$count-3])){			$domain = "http://".$arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];		}else{			$domain = "http://".$arr[$count-2].".".$arr[$count-1];		}        }    return $domain;    }// end if(!strcmp...)    }// end if(count...)    }// end functionecho get_domain("http://blog.csd.com.hk")."
";echo get_domain("http://www.csd.net/www")."
";echo get_domain("http://csd.net/")."
";echo get_domain("http://csd.net/fdfdfd")."
";


(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜