0) { r"/> 0) { r">

 >  기사  >  백엔드 개발  >  由字符串,提取完整子域名的方法 -php_PHP教程

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

WBOY
WBOY원래의
2016-07-13 10:42:261010검색

具体代码如下:

    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 function
echo get_domain("http://blog.csd.com.hk")."<br/>";
echo get_domain("http://www.csd.net/www")."<br/>";
echo get_domain("http://csd.net/")."<br/>";
echo get_domain("http://csd.net/fdfdfd")."<br/>";


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/635053.htmlTechArticle具体代码如下: 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) { r...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.