我正在使用此功能代码
public function getRootDomain($domain)
{
$domain = explode('.', $domain);
$tld = array_pop($domain);
$name = array_pop($domain);
$domain = "$name.$tld";
return $domain;
}
我得到的输出就像example.com 但我想展示m.example.com或www.example.com 帮助相关这个..thankx
答案 0 :(得分:2)
使用parse_url()
。你会想要host
:
<?php
$url = '//www.example.com/path?googleguy=googley';
// Prior to 5.4.7 this would show the path as "//www.example.com/path"
var_dump(parse_url($url));
?>
The above example will output:
array(3) {
["host"]=>
string(15) "www.example.com"
["path"]=>
string(5) "/path"
["query"]=>
string(17) "googleguy=googley"
}
你会像这样使用它:
public function getRootDomain($domain)
{
$parts = parse_url($domain);
return $parts['host'];
}
如果您使用的是PHP 5.4 +:
public function getRootDomain($domain)
{
return parse_url($domain)['host'];
}