-2

I am new to PHP world and I would like to be able to get any subdomain typed on the URL, for example if the URL is mike.myapp.com I would like to get "mike", if the URL is "james.myapp.com" I would like to get "james" and so on.

How this is done with PHP?

Dylan Wheeler
  • 6,740
  • 14
  • 55
  • 79
medBouzid
  • 6,732
  • 8
  • 49
  • 78

1 Answers1

1

I've done that using this in the past:

$domain = 'myapp.com';

$sub = preg_replace('/\.' . preg_quote($domain) . '/i', '', $_SERVER['HTTP_HOST']);

This effectively gets rid of your domain and TLD and isolates the subdomain name.

You could just as well not using regex and use str_ireplace() replacing ".myapp.com" with an empty string.

drew010
  • 67,054
  • 11
  • 128
  • 154