<?php
function createTLD($cache_filename, $max_tl=2) {
$cache_folder = str_replace(basename($cache_filename), '', $cache_filename);
if (!file_exists($cache_folder) || !is_writable($cache_folder)) {
throw new Exception($cache_folder . ' is not writable!');
}
// feel free to use "fsockopen()" or "curl_init()" if "fopen wrappers" are disabled or "memory_limit" is to low
$tlds = @file('http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1');
if ($tlds === false) {
throw new Exception('effective_tld_names.dat is not readable!');
}
$i = 0;
// remove unnecessary lines
foreach ($tlds as $tld) {
$tlds[ $i ] = trim($tld);
// empty comments top level domains this is overboard
if (!$tlds[ $i ] || $tld[0] == '/' || strpos($tld, '.') === false || substr_count($tld, '.') >= $max_tl) {
unset($tlds[ $i ]);
}
$i++;
}
$tlds = array_values($tlds);
file_put_contents($cache_filename, "<?php\n" . '$tlds = ' . str_replace(array(' ', "\n"), '', var_export($tlds, true)) . ";\n?" . ">");
// feel free to split the file into multiple and smaller files f.e. by first char of the domain-level-name to reduce memory usage
}
function getHost($dom='', $fast=false) {
// general
$dom = !$dom ? $_SERVER['SERVER_NAME'] : $dom;
// for parse_url() ftp:// http:// https://
$dom = !isset($dom[5]) || ($dom[3] != ':' && $dom[4] != ':' && $dom[5] != ':') ? 'http://' . $dom : $dom;
// remove "/path/file.html", "/:80", etc.
$dom = parse_url($dom, PHP_URL_HOST);
// replace absolute domain name by relative (http://www.dns-sd.org/TrailingDotsInDomainNames.html)
$dom = trim($dom, '.');
// for fast check
$dom = $fast ? str_replace(array('www.', 'ww.'), '', $dom) : $dom;
// separate domain level
$lvl = explode('.', $dom);// 0 => www, 1 => example, 2 => co, 3 => uk
// fast check
if ($fast) {
if (!isset($lvl[2])) {
return isset($lvl[1]) ? $dom : false;
}
}
// set levels
krsort($lvl);// 3 => uk, 2 => co, 1 => example, 0 => www
$lvl = array_values($lvl);// 0 => uk, 1 => co, 2 => example, 3 => www
$_1st = $lvl[0];
$_2nd = isset($lvl[1]) ? $lvl[1] . '.' . $_1st : false;
$_3rd = isset($lvl[2]) ? $lvl[2] . '.' . $_2nd : false;
$_4th = isset($lvl[3]) ? $lvl[3] . '.' . $_3rd : false;
// tld check
require('cache/tlds/all.txt'); // includes "$tlds"-Array or feel free to use this instead of the cache version:
//$tlds = array('co.uk', 'co.jp');
$tlds = array_flip($tlds);// needed for isset()
// fourth level is TLD
if ($_4th && !isset($tlds[ '!' . $_4th ]) && (isset($tlds[ $_4th ]) || isset($tlds[ '*.' . $_3rd ]))) {
$dom = isset($lvl[4]) ? $lvl[4] . '.' . $_4th : false;
}
// third level is TLD
else if ($_3rd && !isset($tlds[ '!' . $_3rd ]) && (isset($tlds[ $_3rd ]) || isset($tlds[ '*.' . $_2nd ]))) {
$dom = $_4th;
}
// second level is TLD
else if (!isset($tlds[ '!' . $_2nd ]) && (isset($tlds[ $_2nd ]) || isset($tlds[ '*.' . $_1st ]))) {
$dom = $_3rd;
}
// first level is TLD
else {
$dom = $_2nd;
}
return $dom ? $dom : false;
}
$urls = array(
'21221.dominio.com/blogs-ventajas-caracteristicas/',
'dasdasds.dominio1.net/MySiteEmulator.htm?name=google',
'dominio2.org/que-significa-mi-dominio-y-el-alias/',
);
if (!file_exists('cache/tlds/all.txt')) {// feel free to refresh by interval
createTLD('cache/tlds/all.txt');
}
echo '<pre>';
foreach ($urls as $url) {
echo $url . ':' . var_export(getHost($url), true) . "\n";
}
echo '</pre>';
?>