Solved

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

Posted on 2009-06-29
4
289 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 108

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 108

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 108

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

23 Experts available now in Live!

Get 1:1 Help Now