1

How to get sub domain from URL. let say subdomain.example.com, then i want only subdomain

Example :

https://subdomain.example.com
https://anything.example.com/pathurl

Output :

Subdomain
anything

Trying code from stackoverflow with some modification but did not work :(

$subdomain = substr($_SERVER['SERVER_NAME'],0,4) === '/^([^.]+)/' ? substr($_SERVER['SERVER_NAME'],0) : $_SERVER['SERVER_NAME'];
echo $subdomain;
Stellan Coder
  • 323
  • 1
  • 11

4 Answers4

3

Firstly, to break down your attempt:

  • $_SERVER['SERVER_NAME'] gives you the full server name, e.g. "subdomain.example.com"
  • 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 "stackoverflow.com". That is unavoidable with simple string matching, because there is no standard number of segments that mark a "public domain" - e.g. "bbc.co.uk" is registered at the same level as "bbc.com", 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.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • the problem of this is if there's no subdomain it will return the host – Jerson Jan 07 '22 at 19:08
  • @Jerson Unless you integrate the [Public Suffix List](https://publicsuffix.org/), that is inevitable: there is no universal distinction between "domain" and "sub-domain". I'll edit in a warning to that effect. – IMSoP Jan 07 '22 at 19:10
2

You could use regex.

preg_match('/^[^.]*\.(?=\w+\.\w+$)/',$_SERVER['SERVER_NAME'],$matches);

$subdomain = isset($matches[0]) ? trim($matches[0],'.') : '';

echo $subdomain;

here is the fiddle

Jerson
  • 1,700
  • 2
  • 10
  • 14
  • 1
    This returns empty for anything with more than three segments in the domain, e.g. "softwareengineering.meta.stackexchange.com" or "www.bbc.co.uk"; my answer would return "softwareengineering" and "www" for those; a library using the Public Suffix List would be able to give you "softwareengineering.meta" and "www". – IMSoP Jan 07 '22 at 20:09
  • 1
    [Demo1](https://3v4l.org/PiLPu) and [Demo2](https://3v4l.org/CbE1l) This just does not work – RiggsFolly Jan 07 '22 at 23:08
  • 1
    @RiggsFolly in demo1 op use SERVER NAME so it will not return the scheme, in demo 2 it will empty because there subdomain in the url, but threew segments will not work in this – Jerson Jan 08 '22 at 03:04
  • 1
    there's no subdomain, typo error my bad – Jerson Jan 08 '22 at 03:48
2

Using a mixture of parse_url() and strtok() like this

$url = 'https://anything.example.com/pathurl';

$bits = parse_url($url);
$sub = strtok($bits['host'], '.');
echo $sub;

RESULTS

anything

Demo

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

Thanks to @IMSoP , @RiggsFolly and @Jerson. All of your code work amazing :)

For my own future reference :

$fulldomain = $_SERVER['SERVER_NAME'];
$matches = null; 
preg_match('/^[^.]+/', $fulldomain, $matches);
$subdomain = $matches[0];

echo $subdomain;
Stellan Coder
  • 323
  • 1
  • 11