Solved

How do I pull the subdomain name out of a URL?

Posted on 2009-06-29
4
296 Views
Last Modified: 2012-05-07
How would I get the subdomain name from a web address?

For example, in http://subdomain.domain.com how would I grab the name subdomain from the URL?

0
Comment
Question by:Donnie Walker
  • 3
4 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24738062

// BREAK THE HOST NAME APART AT THE DOTS
$x = explode('.', $_SERVER["HTTP_HOST"]);
$subdomain = $x[0];

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24738074
If you have a separate URL in your code, first explode on the // to isolate the information that is in $_SERVER["HTTP_HOST"]
$url = 'http://subdomain.domain.com'
$x = explode('//', $url);
$host = $x[1];
 
$x = explode('.', $host);
$subdomain = $x[0];

Open in new window

0
 

Author Closing Comment

by:Donnie Walker
ID: 31598019
As always Ray, Thanks.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24739908
Thanks for the points - glad I could help, ~Ray
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now