I use 2 functions to pick out the eventual subdomain:
[man]parse_url[/man] and [man]explode[/man]
The host name= johndoe.domain.com
has 2 points, and I explode it into 3 pieces by each (.)
Then display the first part.
Now it is not easy to know IF there is a subdomain in hostname.
Normally we can count the pieces, and if there are more than 2
then the first part is a subdomain:
- google.com, 2 pieces = no subdomain
- images.google.com, 3 pieces first is subdomain
But then you have those troublesome british. (you know who counts 15,30,40,game in tennis)
- manchester.co.uk, 3 pieces and still not a subdomain 😕
Here is my script anyway.
You should see what's going on, I hope:
<?php
echo '<pre>';
$url = "http://johndoe.domain.com/manual/en/function.parse-url.php";
$u_parts = parse_url($url);
print_r($u_parts);
$hostname = $u_parts['host']; // take hostname part
echo $hostname.'<br />';
$host_parts = explode('.', $hostname); // explode into 3 pieces
print_r($host_parts);
echo 'Parts= '.count($host_parts).'<br />'; // count parts
echo $host_parts[0]; // the first part=johndoe
?>