Alex Rivera | Logout

PHP function to get the subdomain of a URL

Asked 2011-03-13T22:36:05.730
128

Is there a function in PHP to get the name of the subdomain?

In the following example I would like to get the "en" part of the URL:

en.example.com
Edit
Report

2 Answers

1

What I found the best and short solution is

array_shift(explode(".",$_SERVER['HTTP_HOST']));
answered 2012-10-12T07:46:37.730
0

if you are using drupal 7

this will help you:

global $base_path;
global $base_root;  
$fulldomain = parse_url($base_root);    
$splitdomain = explode(".", $fulldomain['host']);
$subdomain = $splitdomain[0];
answered 2013-07-24T12:33:13.633

Your Answer