Page 1 of 1

Getting TLD from domain input

Posted: Thu Jun 10, 2004 6:10 am
by evilcoder
Hey guys, how would i extract a 3 letter TLD from a string?
eg:

$domain = "somesite.net";

i want to be able to separate this string into 2 variables:

$host & $tld

Thanks guys. :)

Posted: Thu Jun 10, 2004 6:17 am
by feyd

Code: Select all

preg_match('|^(.*)\.([^.]{2,})$|',$domain,$matches);

print_r($matches);
I think...

Posted: Thu Jun 10, 2004 6:17 am
by kettle_drum
explode(".", $domain); then take the last item in the array, check to see if its 3 letters, if its 2 take the one before as well so you can get stuff like .com/.org/co.uk/.org.uk etc

Posted: Thu Jun 10, 2004 7:14 am
by evilcoder
Thanks guys, i ended up using kettle's solution and added count.

Code: Select all

<?php

$domain = "strip.com.au";
$lines = explode(".", $domain);
$count = sizeof ( $lines );
echo $lines[1] . "." . $lines[2];
?>

Posted: Thu Jun 10, 2004 10:05 am
by feyd
wouldn't it be more like:

Code: Select all

<?php

$domain = "strip.com.au";

$domains = explode('.',$domain);

if(strlen($domains[sizeof($domains) - 1]) == 2)
{
  $tld = $domains[sizeof($domains) - 2].'.'.$domains[sizeof($domains) - 1];
}
else
{
  $tld = $domains[sizeof($domains) - 1];
}
?>

Posted: Thu Jun 10, 2004 9:23 pm
by evilcoder
Yeah, thats better. Thanks mate.

Posted: Thu Jun 10, 2004 9:45 pm
by feyd
should probably toss a sanity check in there two.. (for at least 1 dot..) :)