热门关键字:  PHP  Cisco  seo  网络广告 虚拟主机 中文域名
当前位置 :| 主页>编程开发>PHP>php基础>

PHP正则表达式从url中取得域名

来源:天新网 作者: 时间:2008-08-04 点击:

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

例如:http://souzz.net/html/database/ORACLE/69613.html

得到souzz.net

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


最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
赞助商连接