4

I am having a list of array of domains from which I want to get only the name of the domain so the list is something like this :

$domain_name = [
            "domain",
            "somedomain",
            "mydomain",
            "yourdomain"
        ];

And i will be using $_SERVER['HTTP_REFERER'] to catch a domain name. From which I just want to get the domain base name with any www. or .com/ .in/ .io/ .net / .org e.t.c or any tralling slash in the URL. Just want to know the basic domain name. Is there any way to do that in php?

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
user3201500
  • 1,538
  • 3
  • 22
  • 43
  • 2
    `parse_url()` ? – frz3993 Jul 21 '18 at 18:37
  • I believe this question has already been asked. – Angel Politis Jul 21 '18 at 18:37
  • parse_url will give me the hostname. But I am not sure if I can only get the domain from `www.mydomain.co.in` I want to get only `mydomain`. Is there any other way? – user3201500 Jul 21 '18 at 18:40
  • `array_map(function($e){ return parse_url($e, PHP_URL_HOST);}, $domains);` – felipsmartins Jul 21 '18 at 18:41
  • @AngelPolitis can you give me a reference? I tried searching but didn't got anything. – user3201500 Jul 21 '18 at 18:41
  • @felipsmartins this won't help. As parse_url will provide full url like "www.domain.com" and I only want "domain" – user3201500 Jul 21 '18 at 18:43
  • @user3201500 ` array_map(function($e){ return explode('.', parse_url($e, PHP_URL_HOST))[1];}, $domains); – felipsmartins Jul 21 '18 at 18:51
  • @NigelRen I just don't want only host. I want only the name of the domain. – user3201500 Jul 21 '18 at 18:59
  • Voting to reopen. Given `http://sub.example.com`, OP wants `example`, while `parse_url` can at best supply the `host` which is `sub.example.com`. @user3201500 I think you could reduce the question down to just that--an input, desired output, and reason `parse_url` does not work. – FThompson Jul 21 '18 at 19:06
  • Read the [`parse_url()` documentation in the manual](http://php.net/manual/en/function.parse-url.php) and add whatever parameter you are interested in to it – RiggsFolly Jul 21 '18 at 19:13
  • @RiggsFolly I've read the documentation and it offers `host` which returns `subdomain.domain.com` while OP wants just `domain`. It seems that `parse_url` doesn't have an option for just the domain. Am I missing something? – FThompson Jul 21 '18 at 19:18
  • @Vulcan no you are not. you got it exactly what I want. But I think I have to use some regular expression to remove these. No other option left. – user3201500 Jul 21 '18 at 19:46
  • @user3201500 When the question was closed, I was in the middle of writing an answer using the code in [this `parse_url` comment](http://php.net/manual/en/function.parse-url.php#83828). With that function, you can get the full domain and extension to remove the extension, leaving just the domain. See my sandbox code using that function: http://sandbox.onlinephpfunctions.com/code/9284e27674bb1781c2fed7fc0c7ad71721b827aa If this question is reopened, I'll post that as a proper answer. – FThompson Jul 21 '18 at 19:56
  • Its amazing @Vulcan its working! I think you should put this as an answer – user3201500 Jul 21 '18 at 20:42
  • @user3201500 I can't post an answer while the question is closed :( glad the solution works for you though. – FThompson Jul 21 '18 at 20:59

0 Answers0