I recommend using the Zend \ Validator \ Hostname validator. Example from the documentation:
$hostname = 'http://google.com'; $validator = new Zend\Validator\Hostname(Zend\Validator\Hostname::ALLOW_DNS); if ($validator->isValid($hostname)) { // hostname appears to be valid echo 'Hostname appears to be valid'; } else { // hostname is invalid; print the reasons foreach ($validator->getMessages() as $message) { echo "$message\n"; } }
source share