URL validation is a hard thing to do. There are so many variations that it is nearly impossible to predict how it may be structured.
Luckily, the open-source brings some smart guys capable of simplifying these dilemmas for us with regex.
Diego Perini created a regex that I’m really impressed with and could not resist sharing in a post. He shared it for JS, but I’ve converted it for PHP, since I need it mainly for WordPress.
The PHP version:
function isUrl($url) {
$pattern = '%^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\x{00a1}-\x{ffff}][a-z0-9\x{00a1}-\x{ffff}_-]{0,62})?[a-z0-9\x{00a1}-\x{ffff}]\.)+(?:[a-z\x{00a1}-\x{ffff}]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$%iu';
return !empty(preg_match($pattern, trim($url)));
}