Home > Backend Development > PHP Tutorial > PHP regular expression to get domain name from url_PHP tutorial

PHP regular expression to get domain name from url_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 14:57:53
Original
963 people have browsed it

以下代码可以从URL中获得域名

例如:http://bkjia.com/live/q.html

得到bkjia.com

以下为引用的内容:
以下为引用的内容:
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)){
$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
}else{
$domain = $arr[$count-2].".".$arr[$count-1];
}
return $domain;
}// end if(!strcmp...)
}// end if(count...)
}// end function
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) { }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)){ $domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
}else{ $domain = $arr[$count-2].".".$arr[$count-1]; }
return $domain;
}// end if(!strcmp...) }// end if(count...) }// end function
http://www.bkjia.com/PHPjc/363986.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/363986.htmlTechArticle以下代码可以从URL中获得域名 例如:http://liehuo.net/live/q.html 得到liehuo.net 以下为引用的内容: function get_domain($url){ $pattern = /[w-]+.(com|net|org...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template