By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,831 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,831 IT Pros & Developers. It's quick & easy.

Regexp to find the subdomain (if any) from $HTTP_HOST

P: n/a
I need help creating a regular expression to find the subdomain in the
$HTTP_HOST variable.

The domain is always in this form:

www.domain.com
or
uk.domain.com
or
domain.com

there is no such thing as a
sub.sub.sub.domain.com
(not in my case, at least)

So, I need to find out if the subdomain is empty, "www" or "uk".

Can someone help me?

Leif
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> I need help creating a regular expression to find the subdomain in the
$HTTP_HOST variable.

No need for regular expressions in this case:
<?php
$parts = explode(".",$HTTP_HOST);
$length = count($parts);
$subdomain = $length>2 ? $parts[$length-3] : "";
?>
Martin
Jul 16 '05 #2

P: n/a
Leif Wessman wrote:

I need help creating a regular expression to find the subdomain in the
$HTTP_HOST variable.

The domain is always in this form:

www.domain.com
or
uk.domain.com
or
domain.com

there is no such thing as a
sub.sub.sub.domain.com
(not in my case, at least)

So, I need to find out if the subdomain is empty, "www" or "uk".

Can someone help me?

Leif


Untested:

$foo = preg_replace("/^(http:\/\/)?([^\.]+)?(\.?domain\.com.*)$", "\$2",
$HTTP_HOST);
switch($foo) {
case "www":
//statements
break;
case "uk":
//statements
break;
case "":
//statements
break;
default:
echo "Unrecoginzed URL";
}
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.