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

WBOY
Release: 2016-07-25 08:59:47
Original
1715 people have browsed it
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);
?>
Copy after login

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";  
?>
Copy after login


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!