Skip to content

Instantly share code, notes, and snippets.

@JanPetterMG
Created January 19, 2016 15:50
Show Gist options
  • Save JanPetterMG/51d5760aa45f0d7d6913 to your computer and use it in GitHub Desktop.
Save JanPetterMG/51d5760aa45f0d7d6913 to your computer and use it in GitHub Desktop.
Validate URL hostname
/**
* Validate host name
*
* @link http://stackoverflow.com/questions/1755144/how-to-validate-domain-name-in-php
* @param string $host
* @return bool
*/
function isValidHostName($host)
{
return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $host) //valid chars check
&& preg_match("/^.{1,253}$/", $host) //overall length check
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $host) //length of each label
&& !filter_var($host, FILTER_VALIDATE_IP)); //is not an IP address
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment