function validate_url ($url
{
$regex = '/^(https?|ftp)://'; //protocol
$regex .= '(([a-z0-9$_.+!*'(),;?&=-]|%[0-9a-f]{2})+'; //username
$regex .= '(:([a-z0-9$_.+!*'(),;?&=-]|%[0-9a-f]{2})+)?'; //password
$regex .= '@)?'; //auth requires @
$regex .= '((([a-z0-9][a-z0-9-]*[a-z0-9].)*'; //domain segments AND
$regex .= '[a-z][a-z0-9-]*[a-z0-9]'; //top level domain OR
$regex .= '|((d|[1-9]d|1d{2}|2[0-4][0-9]|25[0-5]).){3}'
$regex .= '(d|[1-9]d|1d{2}|2[0-4][0-9]|25[0-5])'; //IP address
$regex .= ')(:d+)?'; //port
$regex .= ')(((/+([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)*'; //path
$regex .= '(?([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)'; //query string
$regex .= '?)?)?'; //path and query string optional
$regex .= '(#([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)?'; //fragment
$regex .= '$/i'
return (preg_match$regex, $url) ? true : false);
}