Check Valid Domain Name and Valid TLD in PHP

Tuesday, August 16, 2011

Check the domain name is valid Domain Name and Domain extension is valid TLD.  When working this task I want to find the solution to check the domain name is valid TLD and the valid domain name. with the power of Regular Expression. Here is the solution to check the given domain name is valid TLD and valid name.

Following function will full fill the following needs:
  • Domain Name Must contain Alpha Numeric 
  • Only special character (-) hyphen. is allowed on domain names.
  • Check the generic domain extension (.com, .edu, .gov, .int, .mil, .net, and .org)
  • All International domain extensions (TLDs) approved by ICANN

valid-domain-name.php
<?php

// @ Desc: Function to check the Given Domain Name is Valid Domain Name and Valid TLD
// @ Parm: domain name : eg: example.com
// @ Return: 0 or 1 (for Valid Domain Name)
function is_valid_domain_extension($domain)
{
	return preg_match('/^([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i',$domain);
}

// Example example.mz 
// .MZ the domain for Republic of Mozambique
// I aware of the Region when I am working for this function.
echo is_valid_domain_extension("example.mz"); // 

?>

1 comment:

Christopher Hunt said...

ICANN's new gTLDs will break this nice bit of code. Of course, it will break lots of things....

Post a Comment