使用PHP获取根域名和网站子域名

I am using this function code

public function getRootDomain($domain)
{
$domain = explode('.', $domain);

$tld = array_pop($domain);
$name = array_pop($domain);

$domain = "$name.$tld";

return $domain;
}

And the output i get is something like example.com but i want to show m.example.com or www.example.com Help related this ..thankx

Use parse_url(). You would want the 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"
}

You would use it like so:

public function getRootDomain($domain)
{
    $parts = parse_url($domain);
    return $parts['host'];
}

If you're using PHP 5.4+:

public function getRootDomain($domain)
{
    return parse_url($domain)['host'];
}