Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-06-29
4
Medium Priority
?
321 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

609 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