加入收藏 | 设为首页 | 会员中心 | 我要投稿 淮安站长网 (https://www.0517zz.cn/)- 运营、云管理、经验、智能边缘、云硬盘!
当前位置: 首页 > 教程 > 正文

php 从字符串中获取URL中获得域名

发布时间:2022-06-20 08:53:57 所属栏目:教程 来源:互联网
导读:本教程主要主讲从字符串中得出url的域名部份,下面来看看吧。希望对我有帮助。 例如:http://www.phpfensi.com 得到 www.phpfensi.com function get_domain($url){ $pattern = /[w-] .(com|net|org|gov|cc|biz|info|cn)(.(cn|hk))*/; preg_match($pattern, $u
  本教程主要主讲从字符串中得出url的域名部份,下面来看看吧。希望对我有帮助。
 
  例如:http://www.phpfensi.com  得到  www.phpfensi.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 

(编辑:淮安站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读