如何将域名拆分为sld和tld(扩展名)?

The domain's need to be returned as the actual domain name, and the extension, separately

http://www.something.com

should return: sld = something, tld= com

something.co.uk

should return: sld = something, tld= co.uk

I am not much familiar with regular expressions, so I really need some help in handling this.

I suppose I can use parse_url(), and check the host, but what then?

Just as you said, you can use $urlCompontents=parseUrl($url) to get the hostname. Then you could use explode(".",$urlCompontents["host"]) to split the hostname into the different parts, e.G. array("example","co","uk"). You'll have to do the rest by comparing the parts against a list, because there is no fixed rule that e.G. "uk" by itself is not considered a TLD but "co.uk" is. But you don't need any regular expressions here.

Split the string on . characters (no need to regex), then work through the resulting array from the end.

You'll need to manually keep an index of which SLDs are sold directly to end users as there is no simple pattern that describes them accurately.

Keep in mind that there is likely to be an influx of new TLDs.

Below code will split (explode) the host string on '.' character. A simple exception array of tld's is needed and I put already co.uk in it. And only for these exceptions it will use the last two chunks of the host name.

$h='something.co.uk';
$x=array('uk'=>'co'); // exceptions of tld's with 2 parts
$r=explode('.',$h); // split host on dot
$t=array_pop($r); // create tld
if(isset($x[$t]) and end($r)==$x[$t]) $t=array_pop($r).'.'.$t; // add to tld for the exceptions
$d=implode('.',$r); // domain
echo "sld:$d, tld:$t";

The result is sld:something, tld:co.uk

Here is what I use. Hope it helps.

function extractTLD( $domain )
{
    $productTLD = '';
    $tempstr = explode(".", $domain);
    unset($tempstr[0]);
    foreach($tempstr as $value){
        $productTLD = $productTLD.".".$value;
    }    
    return $productTLD;
}
$pos = strpos('domain.com', '.');
$length= strlen('domain.com');
$domain = substr('domain.com', 0, $pos);
$tld= substr('domain.com', $pos, $length);

Use parse_url($url,PHP_URL_HOST) to get the host name; then use the function below to split the domain into parts:

function split_domain($host,$SLDs='co|com|edu|gov|mil|net|org')
{
    $parts=explode('.',$host);
    $index=count($parts)-1;
    if($index>0 && in_array($parts[$index-1],explode('|',$SLDs))) $index--;
    if($index===0) $index++;
    $subdomain=implode('.',array_slice($parts,0,$index-1));
    $domain=$parts[$index-1];
    $tld=implode('.',array_slice($parts,$index));
    return array($subdomain,$domain,$tld);
}

Just in case someone needs to get an updated list of valid TLD's: http://data.iana.org/TLD/tlds-alpha-by-domain.txt

Just use the PHP Explode Function with a limit of two.

Example 1:

var_dump(explode('.','example.com',2));

Example 1 Result:

array(2) { [0]=> string(7) "example" [1]=> string(3) "com" }

Example 2:

var_dump(explode('.','example.uk.com',2));

Example 2 Result:

array(2) { [0]=> string(7) "example" [1]=> string(6) "uk.com" }