=3 && $tlds[0][0]=='#' && preg_match('#^\d+$#',$tlds[1])===1) { array_shift($tlds); $ts=array_shift($tlds); if (preg_match('#^\d+$#',$ts)===1) { foreach ($tlds as $val) { if (preg_match('#^[a-z\-\d]+$#',$val)!==1) { $warnings[]="TLDs cache file «{$cachefp}» had unexpected format (3)"; $tlds=false; break; } } } else { $warnings[]="TLDs cache file «{$cachefp}» had unexpected format (2)"; $tlds=false; } } else { $warnings[]="TLDs cache file «{$cachefp}» had unexpected format (1)"; $tlds=false; } $now=time(); // if cache file doesn't exist, is not readable or has wrong format, or if its timestamp is more than 1 day old, try to load tlds from the web if ($tlds===false || $now-$ts>86400) { $url='https://data.iana.org/TLD/tlds-alpha-by-domain.txt'; $tlds=@file($url,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); if ($tlds===false) { $warnings[]="could not load TLDs from «{$url}»"; } else { if ($tlds[0][0]=='#') array_shift($tlds);// remove first line if it's comment foreach ($tlds as $key=>$val) { if (preg_match('#^[A-Z\-\d]+$#',$val)!==1) { echo "{$val}\n"; $tlds=false; break; } else { $tlds[$key]=strtolower($val); } } if ($tlds===false) $warnings[]="TLDs from «{$url}» had unexpected format"; } } if ($tlds===false) { $tlds=$deftlds; $warnings[]='resorted to embedded TLDs (1)'; } else { if (@file_put_contents($cachefp,"# This file is automatically generated, please do not modify it\n{$now}\n".implode("\n",$tlds)."\n")===false) { $warnings[]="could not write TLDs into cache file «{$cachefp}»"; $tlds=$deftlds; $warnings[]='resorted to embedded TLDs (2)'; } // uncomment next line to output tlds for $deftlds // echo "'".implode("',\n'",$tlds)."'\n"; } if ($regexformat) { rsort($tlds); $tlds=implode('|',$tlds); } (count($warnings)==0) ? $warnings=null : $warnings=implode('; ',$warnings); return ['tlds'=>$tlds, 'warnings'=>$warnings]; } ?>