Validate a Domain Name

I was looking for a decent way to validate against a domain name (not a url) but could not see one in either Drupal or Codeigniter so I quickly built this function

/**
 * function to check for valid domain names
 */
function validate_domain_name($domain) {
  // Strip out any http or www
  $search = array('http://', 'www.');
  $replace = array('','');
  $domain = str_replace($search, $replace, $domain);

  $regex = "/^([a-z0-9][a-z0-9\-]{1,63})\.[a-z\.]{2,6}$/i";

  return preg_match($regex, $domain, $matches);
}

Happy coding !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>