Home > Article > Backend Development > An example of php code to obtain the domain name in the URL
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"; ?> |