Solved

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

Posted on 2009-06-29
4
276 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
Comment Utility

// 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
Comment Utility
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
Comment Utility
As always Ray, Thanks.
0
 
LVL 108

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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…

763 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

9 Experts available now in Live!

Get 1:1 Help Now