0

Currently I am using this code to get the domain name (without www. or domain ending like .com):

explode('.', $url)[1];

Due to the fact that this code is in a loop it takes very long to handle it. Furthermore it can not get "example" from http://example.com/asd/asd.asd.html. Is there another and faster way to solve this?

Thank you for any answer in advance!

best greetings

redsunset
  • 49
  • 1
  • 8

1 Answers1

0

use parse_url()

$host = parse_url($url, PHP_URL_HOST);

PHP_URL_HOST returns the Host

Further, use a Regex to get the desired Part of the Host:

$result = preg_match('/^(?:www\.)?([^\.]+)/', $match);
Bernhard
  • 1,852
  • 11
  • 19
  • thanks! but this does not handle the domain name problem – redsunset Jul 28 '17 at 08:07
  • You're welcome! The Problem is: Subdomains versus Domains like .com.au. This is not easy to manage. You can have a Domain like: www.example.com or subdomain.example.com or example.com.au or subdomain.example.com.au. This is not manageable by that easy imho. – Bernhard Jul 28 '17 at 10:00
  • 2
    You need to use the public suffix list to accomplish this https://publicsuffix.org/ – Emmanuel Mathi-Amorim Jul 28 '17 at 12:52