PHP: Validate a domain name.

Posted October 2nd, 2010


The following function validates and returns TRUE or FALSE if the supplied string could be a valid domain name format.

<?php
function validUrl($url) {
    if (preg_match('/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i', $url)) {
        return TRUE;
    else
        return FALSE;
    }
}
?>