Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

Best way to extract the MAIN DOMAIN from a URL

Hello everyone!

I'm looking for THE BEST & MOST RELIABLE way to extract the main domain name from URLS. Example:

https://www.123-456-789-012.cprapid.co.uk:2088/cpsess4243010201/frontend/jupiter/terminal/index.html
www.bob.bob.bob.bob.bob.co.uk/index.php?page=contacts
com.com/contact.php
https://www.sub.com.co.uk/index.php?domain=com.com

When passed it should return

cprapid.co.uk
bob.co.uk
com.com
com.co.uk

and so on...

I'm currently using

function get_domain($url) {
    $pieces = parse_url($url);
    $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
    if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{0,63}\.[a-z\.]{1,5})$/i', $domain, $regs)) {
    return $regs['domain'];
    }
    return false;
}

But it really fails sometimes.

I'm even ready to pay for a function that is 100% reliable.

0 likes
6 replies
click's avatar

If you are not allowed to use a "package" you can still look at their code.

There are a lot of people asking the same questions on stackoverflow, a google search will give you the answer.

What you need is a list of all the tld's. https://publicsuffix.org/list/public_suffix_list.dat

With this list you are able to determine the root domain.

{subdomain with dots}.{root domain without dots}.{tld with dots}/{path} 
lat4732's avatar
Level 12

@click I'm already using this

$domain = str_replace('www.', '', $request->text);
                
if(strpos($domain, 'http') === false) {
      $domain = "http://" . $domain;
}
               
$domain = strtolower($domain);

$domain = parse_url($domain, PHP_URL_HOST);

$web_seo_url = get_domain($domain);

$domain_explode = explode('.', $web_seo_url);

$suffix_array = [];

$suffix_list = \Http::get('https://publicsuffix.org/list/public_suffix_list.dat')->body();

foreach(preg_split("/((\r?\n)|(\r\n?))/", $suffix_list) as $line) {
    if(!str_starts_with($line, '//')) {
        if($line != "") {
            array_push($suffix_array, $line);
        }
    }
} 

if(in_array(end($domain_explode), $suffix_array) || in_array((count($domain_explode) > 2) ? $domain_explode[count($domain_explode)-2] . "." . end($domain_explode) : end($domain_explode), $suffix_array)) {
    // continue....
}

Please or to participate in this conversation.