PHP Getting Domain Name from Subdomain

PHP Getting Domain Name From Subdomain

Stackoverflow Question Archive:

  • How to get domain name from url?
  • Check if domain equals value?
  • How do I get the base url?

print get_domain(""); // outputs ''

function get_domain($url)
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
return false;

Trying to get subdomain URL using php

Here's a one-liner to get the subdomain:

$subdomain = join('.', explode('.', $_SERVER['HTTP_HOST'], -2))

explode with the limit parameter of -2 will split the string on each dot, ignoring the last two elements. If there are two or fewer elements, it returns an empty array.

join will assemble the resulting array back into a dot delimited string. In case you have multiple subdomains set, e.g. will return

If there is no subdomain, it will return an empty string.

How to get domain name from URL with PHP?

Just use built-in php function parse_url

You can filter a subdomain from a hostname like this

$url = '';

$data = parse_url($url);

$host = $data['host'];

$hostname = explode(".", $host);
$domain = $hostname[count($hostname)-2] . "." . $hostname[count($hostname)-1];

print $domain;

Will output

If you have an url with a port, parse_url will deal with it easily, example

$url = '';

$data = parse_url($url);


Will output

[scheme] => http
[host] =>
[port] => 88
[path] => /testing

And below you check if the hostname is a valid IP address or not

$url = '';

$data = parse_url($url);


$hostIsIpAddress = ip2long($data['host']) !== false;


Which will output bool(true) or bool(false) respectively

Getting domain name without TLD

Group the first part of your 2nd regex into /([^.]+)\.[^.]+$/ and $matches[1] will be php

Get domain and subdomain from $_SERVER[ SERVER_NAME ] (or other way)


$parts=explode('.', $_SERVER["SERVER_NAME"]);

you can split it by the dots.

Get Sub-domain with PHP

Firstly, to break down your attempt:

  • $_SERVER['SERVER_NAME'] gives you the full server name, e.g. ""
  • substr($_SERVER['SERVER_NAME'],0,4) gives you the first four characters of that string; it's likely the example you copied from was looking for "www.", which is four characters long, but "subd" isn't going to tell you much
  • === just compares that two things are identical; since you've just asked for four characters, it's never going to match a string of any other length
  • '/^([^.]+)/' looks like a Regular Expression (aka "regex") - a pattern to match strings; to use that in PHP, you need the PCRE family of functions, such as preg_match and preg_replace

Next, the tip I always give: break the problem down. Forget for a moment that you know what a sub-domain is, and notice that you have a string, and want all the text up to the first dot.

One way to get that is therefore to split the string on every dot, and take the first part. For that, you would use the excitingly named explode:

$subdomain = explode('.', $fulldomain, 1)[0];

Another way would be using the regex pattern you found, which reads "starting at the beginning, match anything but a dot, at least once, and capture that part". You can actually skip one pair of brackets, because they're grouping the whole pattern, so just '/^[^.]+/' is enough. To do that, you'd use preg_match, but note that it doesn't return the matched parts, it puts them in an extra array you pass to it:

$matches = null; // will be populated by preg_match
preg_match('/^[^.]+/', $fulldomain, $matches);
$subdomain = $matches[0];

Note: The above will return "stackoverflow" for an input of "". That is unavoidable with simple string matching, because there is no standard number of segments that mark a "public domain" - e.g. "" is registered at the same level as "", but you can't tell that by looking at them. Unless you know in advance what your possible endings will be, you need something that checks the Public Suffix List, such as the php-domain-parser library.

PHP how to get the base domain/url?


echo $_SERVER['SERVER_NAME']; //Outputs

Get domain name without www and .com in PHP

I try this it work for me.

    $original_url=""; //try with all urls above

$pieces = parse_url($original_url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
echo strstr( $regs['domain'], '.', true );

Output- example

I get this from Here
Get domain name from full URL

Related Topics

Leave a reply