Validate a URL.
boolean Core_Valid::url( string $url )
参数列表
参数 类型 描述 默认值 $url
string
URL
boolean
public static function url($url)
{
// Based on http://www.apps.ietf.org/rfc/rfc1738.html#sec-5
if ( !preg_match(
'~^
# scheme
[-a-z0-9+.]++://
# username:password (optional)
(?:
[-a-z0-9$_.+!*\'(),;?&=%]++ # username
(?::[-a-z0-9$_.+!*\'(),;?&=%]++)? # password (optional)
@
)?
(?:
# ip address
\d{1,3}+(?:\.\d{1,3}+){3}+
| # or
# hostname (captured)
(
(?!-)[-a-z0-9]{1,63}+(?<!-)
(?:\.(?!-)[-a-z0-9]{1,63}+(?<!-)){0,126}+
)
)
# port (optional)
(?::\d{1,5}+)?
# path (optional)
(?:/.*)?
$~iDx', $url, $matches) ) return false;
// We matched an IP address
if ( !isset($matches[1]) ) return true;
// Check maximum length of the whole hostname
// http://en.wikipedia.org/wiki/Domain_name#cite_note-0
if (strlen($matches[1]) > 253) return false;
// An extra check for the top level domain
// It must start with a letter
$tld = ltrim(substr($matches[1], (int)strrpos($matches[1], '.')), '.');
return ctype_alpha($tld[0]);
}