I have to write a function to parse variables that have domains. It is best I explain this by having an example, the variable could contain these things:





However when passed through my function many of these must return either example.com or example.co.united kingdom, the main domain title essentially. I am sure I have carried this out before but I have been looking Google for around twenty minutes and should not find anything. Any help could be appreciated.

EDIT: Disregard the .co.united kingdom, presume that domain names dealing with this function possess a 3 letter TLD.

Stackoverflow Question Archive:

print get_domain("http://somedomain.co.united kingdom") // results 'somedomain.co.uk'

function get_domain($url)

.[a-z.])$/i', $domain, $regs)) 

  return false

I'd make a move such as the following:

// hierarchical variety of top level domain names

$tlds = array(

    'com' => true,

    'uk' => array(

        'co' => true,

        // …


    // …


$domain = 'here.example.co.uk'

// split domain

$parts = explode('.', $domain)

$tmp = $tlds

// travers the tree backwards order, from to left

foreach (array_reverse($parts) as $key => $part)  else 

// build the end result

var_dump(implode('.', array_slice($parts, - $key - 1)))

Probably, what you are searching for is:

http://world wide web.dkim-status.org/regdom-libs/

It is a PHP library that employs the (as nearly out of the box practical) full listing of various TLD's that's collected at publicsuffix.org/list/ , and systems up inside a spiffy little function.

When the library is incorporated, it's as simple as:

$registeredDomain = getRegisteredDomain( $domain )

    $full_domain = $_SERVER['SERVER_NAME']

$just_domain = preg_replace("/^(.*.)?([^.]*..*)$/", "$2", $_SERVER['HTTP_HOST'])

Regex can help you available. Try something similar to this:

([^.]+(.com.co.united kingdom))$

I believe your condition is you haven't clearly defined just what you would like the function to complete. Out of your good examples, you don't would like it to just blindly return the final two, or last three, aspects of the title, but simply understanding what it should not do is not enough.

Here's my prediction at what you want: you will find certain second-level domains, like co.united kingdom., that you might want to become treated like a single TLD (top-level domain) for reasons of the function. For the reason that situation I'd suggest enumerating these kinds of cases and putting them as secrets into an associative array with dummy values, together with all of the normal top-level domain names like com., internet., info., etc. Then whenever you receive a new domain title, extract the final two components if the resulting string is within your array like a key. Otherwise, extract only the last component and make certain that's inside your array. (If even that is not, it isn't a legitimate domain title) In either case, whatever key you need to do get in the array, take that plus yet another component from the finish from the domain title, you'll also find your base domain.

You can, possibly, make things a little simpler by writing a function, rather than utilizing an associative array, to inform if the latter components ought to be treated like a single "effective TLD." The function would most likely consider the next-to-last component and, whether it's shorter than 3 figures, choose that it ought to be treated included in the TLD.