Method to extract complete subdomain name from string -php_PHP tutorial

WBOY
Release: 2016-07-13 10:42:26
Original
1061 people have browsed it

The specific code is as follows:

    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/>";
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/635053.htmlTechArticleThe specific code is as follows: 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 ...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template