473,385 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
2 4869
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Felix Collins | last post by:
Hi, I'm not a regexp expert and had a bit of trouble with the following search. I have an "outline number" system like 1 1.2 1.2.3 1.3
4
by: Yves-Alain NICOLLET | last post by:
I have a script that opens a small window containing a button which when clicked performs a find to the next occurence of a string. It works well when the string is made of only one word or of...
1
by: clusardi2k | last post by:
Hello, I have a shared drive on SGI, Linux, and Windows. The fact that I'm using a shared drive may be mute information. The problem is within the same program a second call to fopen does not...
5
by: jfbarro | last post by:
Hi! I am a completly newbie to regexp and I would like to find a regular expression which execute the following search: "Show me all *Exception* since they are different from EOFException and...
2
by: Praveen | last post by:
In the ISPF editor I am using, for a particullar PO Dataset I am getting the result of FIND statement narrowed down to the colums 48-56. i.e. if I give "FIND 'TO' ALL", the result I am getting as...
13
by: Chua Wen Ching | last post by:
Hi there, I saw this article here in vb.net. http://www.error-bank.com/microsoft.public.dotnet.languages.vb.1/148992_Thread.aspx and ...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
1
by: ravichoudhari | last post by:
how can find if any application is trying to open a file with particular extension (in vc++ 6)
5
by: Morten Snedker | last post by:
At the moment I wish to figure out how to perform a mouse click at a given position on the screen. Someone probably has the solution, but I rather want to learn to find out myself. So, could...
2
by: notnorwegian | last post by:
i want to search a document for a particular regexp and then store that regexp to a file. but search and match only returns matchobjects(what are those anyway? i dont get what to do with them,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.