-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,928
  • 14
  • 56
  • 80
medBouzid
  • 7,484
  • 10
  • 56
  • 86

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
  • 68,777
  • 11
  • 134
  • 162