PHPRO.ORG

If you need to validate a domain name for use in a whois, this little classlet will check that it is valid before you search a whois database. This is not intended to check for sub-domains, simply domains of type example.com.au, example.com etc.


<?php

class parse_domain{

private 
$tldArray;

private 
$countryArray;

public function 
__construct(){

  
$this->tldArray $this->setTldArray();
  
$this->countryArray $this->setCountryArray();
}

/**
 * set the tldArray
 *
 * @access private
 *
 * @return array
 *
 */
private function setTldArray(){
 return array(
"name""mobi""pro""edu""int""com""net""org""museum""csiro""mil""gov""biz""info""root""arpa""aero""cat""jobs""travel");
}

/**
 * Set the countryArray array
 *
 *@access private
 *
 * @return array
 *
 */
private function setCountryArray(){

 return array(
"AF","AL","DZ","AS","AD","AO","AI","AQ","AG","AR","AM","AW","AU","AT","AZ","BS","BH","BD","BB","BY","BE","BZ","BJ","BM","BT","BO","BA","BW","BV","BR","IO","BN","BG","BF","BI","KH","CM","CA","CV","KY","CF","TD","CL","CN","CX","CC","CO","KM","CG","CD","CK","CR","CI","HR","CU","CY","CZ","DK","DJ","DM","DO","TP","EC","EG","SV","GQ","ER","EE","ET","FK","FO","FJ","FI","FR","FX","GF","PF","TF","GA","GM","GE","DE","GH","GI","GR","GL","GD","GP","GU","GT","GN","GW","GY","HT","HM","VA","HN","HK","HU","IS","IN","ID","IR","IQ","IE","IL","IT","JM","JP","JO","KZ","KE","KI","KP","KR","KW","KG","LA","LV","LB","LS","LR","LY","LI","LT","LU","MO","MK","MG","MW","MY","MV","ML","MT","MH","MQ","MR","MU","YT","MX","FM","MD","MC","MN","MS","MA","MZ","MM","NA","NR","NP","NL","AN","NC","NZ","NI","NE","NG","NU","NF","MP","NO","OM","PK","PW","PA","PG","PY","PE","PH","PN","PL","PT","PR","QA","RE","RO","RU","RW","KN","LC","VC","WS","SM","ST","SA","SN","SC","SL","SG","SK","SI","SB","SO","ZA","GS","ES","LK","SH","PM","SD","SR","SJ","SZ","SE","CH","SY","TW","TJ","TZ","TH","TG","TK","TO","TT","TN","TR","TM","TC","TV","UG","UA","AE","GB","US","UM","UY","UZ","VU","VE","VN","VG","VI","WF","EH","YE","YU","ZM","ZW");
}

/**
 *
 * Check there are no invalid chars
 *
 * @access private
 *
 * @param string
 * 
 * @return bool
 *
 */
private function checkChars($domainName){
if(!
preg_match("/^(?:[^\W_]((?:[^\W_]|-){0,61}[^\W_])?\.)+[a-zA-Z]{2,6}\.?$/"$domainName))
    {
    throw new 
Exception("Invalid Characters in Domain Name");
    }
}

/**
 *
 * Check if domain name is valid
 *
 * @access public
 *
 * @param string
 *
 * @return bool
 *
 */
public function validateDomain($domainName){
  
$this->checkDots($domainName);
  
$this->checkParts($domainName);
  
$this->checkChars($domainName);
  return 
true;
}

/**
 *
 * Check the parts of the domain name
 *
 * @access private
 *
 * @return bool
 *
 */
private function checkParts($domainName){
 
$parts explode("."$domainName);

 
/*** now we have 2 or 3 parts ***/
if(!in_array($parts[1], $this->tldArray))
    {
    throw new 
Exception("Domain has incorrect TLD");
    }
/*** if there are 3 parts, we need to check for a valid country domain ***/
 
if(sizeof($parts) == 3)
    {
    if(!
in_array(strtoupper($parts[2]), $this->countryArray))
        {
        throw new 
Exception("Invalid Country Code");
        }
    }
}


/**
 * Check domain name has 1 or 2 dots
 * 
 * @access private
 *
 * @return void
 *
 */
private function checkDots($domainName){
  if(
substr_count($domainName".") != && substr_count($domainName".") != 1)
    {
    throw new 
Exception("Invalid Domain Name Supplied");
    }
}

/*** end of class ***/

try{

$domainName "foo.com.au";

$domain = new parse_domain;

if(
$domain->validateDomain($domainName) == TRUE)
    {
    echo 
"domain is valid";
    }
else
    {
    echo 
"invalid Domain";
    }
}
catch(
Exception $e)
    {
    echo 
$e->getMessage();
    }
?>