Solved

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

Posted on 2009-06-29
4
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 110

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 110

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 110

Expert Comment

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

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSSQL - Lock Row from reading by other programs 9 61
Checking https returns 301 21 61
PHP: Filling Out/Creating a PDF 29 108
Need guidance for special auto increment values 2 22
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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.

726 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