一直安静

子曰:富而可求也,虽执鞭之士,吾亦为之;如不可求,从吾所好。

« 正则表达式中的特殊字符[转]mysql错误对照表 »

PHP正则表达式,从网址(url)中取得域名

以下代码可以从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

  • 相关文章:
  • quote 1.嘿嘿
  • 不错试了很好用
  • 2008-8-12 7:57:18

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Comments

Recent

Powered By Z-Blog. Copyright 一直很安静 Some Rights Reserved. © 2004-2009