PHP Function to Get the Subdomain of a Url

How to get the subdomain of a URL using PHP?

Uses the parse_url.

$url = 'http://sub.example.com/classname/method/arg';

$parsedUrl = parse_url($url);

$host = explode('.', $parsedUrl['host']);

$subdomain = $host[0];
echo $subdomain;

For multiple subdomains you should do like this

$url = 'http://en.sub.example.com/classname/method/arg';

$parsedUrl = parse_url($url);

$host = explode('.', $parsedUrl['host']);

$subdomains = array_slice($host, 0, count($host) - 2 );
print_r($subdomains);

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. foo.bar.domain.com will return foo.bar.

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

How to get the first subdomain with PHP?

$domain = 'sub.dev.example.com';
$tmp = explode('.', $domain);
$subdomain = current($tmp);
print($subdomain); // prints "sub"

Get Sub-domain with PHP

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.

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("http://somedomain.co.uk"); // outputs 'somedomain.co.uk'

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;
}

Get domain with its subdomain from url

The issue is that parse_url is returning false. Check to make sure you get a response before trying to use it otherwise $host is empty.

<?php
function getDomainFromUrl($url) {
$host = (parse_url($url, PHP_URL_HOST) != '') ? parse_url($url, PHP_URL_HOST) : $url;
return preg_replace('/^www\./', '', $host);
}
echo getDomainFromUrl("http://abc.example.com/") . "\n";
echo getDomainFromUrl("http://www.example.com/") . "\n";
echo getDomainFromUrl("abc.example.com");

Output:

abc.example.com

example.com

abc.example.com

PHP script that forwards by reading the subdomain WHILE using https

As discussed in the comments the problem is "blocked loading mixed active content".

An iframe or its contents can not be loaded over http if the site containing the iframe is loaded via https.



Related Topics



Leave a reply



Submit