Validate URL (FTP, HTTP) using regular expression ( regex )

Home » PHP » Validate URL (FTP, HTTP) using regular expression ( regex )
Date: 2005-08-09
Link: http://php.devquickref.com/php-validate-url-uri-regular-expression-regex.html

Validate URL (FTP, HTTP) using regular expression ( regex )

 
<?php
/**
 * valid_url - validates supplied url with a regular expression ( regex ).
 *
 * URL can be FTP, HTTP secure
 * @param  $url String
 * @return true if the address is valid
 * @author Svetoslav Marinov <dqr@cgiexp.com>
 *
 */
function valid_url$url )
{
    if ( !
preg_match'!^((ht|f)tps?://)?[a-zA-Z]{1}([w-]+.)+([w]{2,5})/?$!i'$url ) )
     {
        return 
false;
    } else
    {
        return 
true;
    }
}
if ( 
valid_url"http://devquickref.com" ) )
{
    echo 
"URL OK";
} else
{
    echo 
"Invalid URL.";
}
?>
 
 
Comments
author: Anonymous date: 2008-07-09 14:12:39
title: Re: Validate URL (FTP, HTTP) using regular expression ( regex )
this doesnt work