-1

I have following urls.

reservation.abchotel.com
booking.abchotels.org
abc1.abc.dev

I want to get sub domain from above urls.

ex:-

.abchotel.com
.abchotels.org
.abc.dev

How I do it? I'm using zend feamwork. Please help me. What is the best solution?

Dinuka Thilanga
  • 4,220
  • 10
  • 56
  • 93

4 Answers4

1

It seems you don't want the subdomain but the domain. Because what you listed are not the subdomains.

The following pieces of knowledge will enable you to successfully deal with domain names.

See also[this question on SO on how to get subdomain(s) from an url.

Community
  • 1
  • 1
markus
  • 40,136
  • 23
  • 97
  • 142
0

If they are always in that form you could do something like the following (assuming $url is set).

$split_url = explode(".", $url);
$subdomain = ".".$split_url[1].".".$split_url[2];

Or did you want to know how to get the URL in the first place too, or to allow for more than 3-level domains?

Alex Hadley
  • 2,125
  • 2
  • 28
  • 50
0

A very simple way to get domain and subdomain :

$parts = explode('.', $_SERVER['HTTP_HOST']);

$domain = '.' . implode( '.', array_reverse( 
              array(
                 array_pop($parts),
                 array_pop($parts)
              )
          );
$subdomain = implode('.',$parts);
Frederik Eychenié
  • 1,253
  • 7
  • 8
0
$url = $_SERVER["SERVER_NAME"];
$replace_domains = array(
  ".abchotel.com" => "",
  ".abchotels.org" => "",
  ".abc.dev" => "");

$url = str_replace(array_keys($replace_domains), array_values($replace_domains), $url);

echo $url;
Bluewind
  • 1,054
  • 7
  • 10