2

I need to get some value from the url..
Like if the url was http://www.random.mysite.com/

Then I somehow want to get random.

But this should work aswell if it use www, http:// or https:// in-front of the url..

So how can this be done?

I tried this: array_shift(explode(".",$_SERVER['HTTP_HOST']));

It works if the url is http://random.mysite.com/, but if I put www in-front, then I get www returned instead?

Any ideas?

1
  • Check if you've got www and if so, just repeat the process again with the rest of the exploded parts? Or you could use a regular expression.
    – lanzz
    Commented Oct 21, 2012 at 11:43

5 Answers 5

4

You can try

$url = 'http://random.mysite.com/';
$url = parse_url($url,PHP_URL_HOST);
$url = strstr(str_replace("www.","",$url), ".",true);
var_dump($url);
3

URLs without http[s]:// in front are not URLs. They're strings.

$hostname = preg_replace('~^https?://(www\\.)?~i', null, $url); // shift scheme and www
$hostname = preg_replace('~#.*$~', null, $hostname); // pop target
$hostname = preg_replace('~\\?.*$~', null, $hostname); // pop query string
$hostname = preg_replace('~/.*$~', null, $hostname); // pop uri
// no guarantee it's a valid hostname, there's more checks you can do here

Try this one. Why three steps? To be explicit so you understand what it does and to cater for complicated cases when you have no / and a ? directly. The order is important too. It could be combined in one strike but you need some testing to do it right... There's so many malformed URLs out there when you scrape a bit.

2

You can use parse_url to get everything after the http:// and https:// first.

Then, use explode to split the domain in different elements. For the special case where it could start with www, check the value of the first item in the exploded array. If the first one is www, then the second one must be the subdomain.

$url = 'http://www.sub.yourwebsite.com';
$parsed = parse_url($url);
$domain = explode('.', $parsed['host']);
$subdomain = '';

if ($domain[0] == 'www')
    $subdomain = $domain[1];
else
    $subdomain = $domain[0];
3
  • This doesn't work for non-http URLs. OP needs to parse URLs without a scheme as well.
    – AKS
    Commented Oct 21, 2012 at 11:49
  • It works fine with https://, https://www., http://, and http://www.. OP, can you let us know if the specified URL is of any other case?
    – Aamir
    Commented Oct 21, 2012 at 11:51
  • I'm not sure what OP needs either. But if the user entered "www.stacoverflow.com" (no http:// here), I think OP wants to get "www" from it. problem is that, parse_url doesn't work with such URLs.
    – AKS
    Commented Oct 21, 2012 at 11:54
2

Depending on what you need it for, a cleaner solution might be to make sure you serve consistent URL's first. To redirect all http://yoursite.com to http://www.yoursite.com put this in your .htaccess file (in root directory assuming your using apache).

RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-site.com [NC]
RewriteRule ^(.*)$ http://www.your-site.com/$1 [L,R=301]

More variations of this code here. This also helps a little with SEO, and is generally good practice.

1

Instead of selecting the first part of the domain and check if it is www, you can also start from the back. After you exploded the domain name, the last part will be 'com' and the part before that is 'yoursite'. The part before that is either 'random' or 'www'. 'www' is a subdomain itself and you could even say that 'www.random' is a different subdomain from 'random'.

Disadvantage is that a TLD can be '.com', but it can be '.co.uk' as well, in which case 'yoursite' would be considered a subdomain, while it isn't really.

What you often see, especially in frameworks or libraries that can be used in multiple sites, is a setting that lets you specify the main host name (yoursite.com). That way, you can easily concluse that everything between protocol:// and that host name is your subdomain.

1
  • can you tell me any library which will extract hostname from url? Commented Dec 24, 2014 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.