首页  >  文章  >  后端开发  >  php获取URL中domain域名的代码一例

php获取URL中domain域名的代码一例

WBOY
WBOY原创
2016-07-25 08:59:471684浏览
如何用php代码获取Url中的domain(域名)呢?本文给大家一个参考代码,有需要的朋友,可以看看哦。

例1,

<?php
/**
 Url中取主机域名
 http://bbs.it-home.org
*/
function getExt($url){   
$arr = parse_url($url);     
$file = $arr['host'];   
$ext = substr($file,strpos($file,".")+1);   
return $ext;
}

//调用示例
$url="http://www.baidu.com/path.php?arg=value#anchor";
$qa=getExt($url);
var_dump($qa);
?>

例2,

<?php  
    // 从 URL 中取得主机名  
    preg_match("/^(http:\/\/)?([^\/]+)/i",  
    " http://www.php.net/index.html", $matches);  
    $host = $matches[2];  
      
    // 从主机名中取得后面两段  
    preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);  
    echo "domain name is: {$matches[0]}\n";  
?>


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn