Sunday, February 17, 2013

validate domain name using php regex

<?php 
function isValidDomainName($name = null) {
    if(isValidString($name)) {
        if(preg_match("/^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$/", getValidString($name))) {
            return true;
        }
    }
    return false;
}

function isValidString($text = null)
{
    if ($text == null) {
        return false;
    }
    if (is_null($text)) {
        return false;
    }
    if (is_string($text)) {
        if (strlen(trim(strval($text)))  0) {
            return true;
        }
    }
    return false;
}
function getValidString($text = null)
{
    if (isValidString($text)) {
        return trim(strval($text));
    }
    return null;
} 
?>

No comments:

Post a Comment