<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
以下代码可以从URL中获得域名 例如:http://yyoo.cn/live/q.html
得到yyoo.cn
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
本文介绍了一种使用PHP从URL中提取域名的方法。通过正则表达式匹配和解析URL,该方法能够准确地获取到域名部分,如从'http://yyoo.cn/live/q.html'中提取出'yyoo.cn'。
633

被折叠的 条评论
为什么被折叠?



