URL Validation RegEx

« Back To Archive

0

Problem Statement:

Checks a string to determine if it is a well-formatted URL (web address).

Dependencies:

None

Example Usage:

if (is_url("http://iconify.it")) {
    // Do some stuff
}
/**
 * validates a URL
 * @param string   The string to validate
 * @return bool    Whether or not the string was a valid URL
 */
function is_url($str) {
    return preg_match(
        "!^((ht|f)tps?\:\/\/)?[a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5})/?$!i",
        $str
    );
}