简体   繁体   中英

Get Root Domain Name and subdomain Of Website using 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'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM