Home  >  Article  >  Backend Development  >  An example of php code to obtain the domain name in the URL

An example of php code to obtain the domain name in the URL

WBOY
WBOYOriginal
2016-07-25 08:59:471732browse
How to use php code to get the domain (domain name) in Url? This article gives you a reference code. Friends in need can take a look.

Example 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);
?>

Example 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";  
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn