Many developers are mixing the idea of whether an URL is valid or “openable”, that is — any browser is able to open it. Having an URL that matches second condition doesn’t actually mean, that such particular URL is valid. This article tries to deal with this little misunderstanding.
There’s a GitHub Gist that matches this situation perfectly. Out of given six example URLs:
five or six can be opened by certain web browsers (try to select each and pick
Go to... from context menu). But, only last one is actually a valid one.
if (filter_var($url, FILTER_VALIDATE_URL) !== false) ...
If, on contrary, you want to check if URL is “openable” then the only way you’re left with, is to actually open it with any PHP url opener or wrapper method and check for returned result. There is absolutely no URL validator that will return true for any URL that any browser is able to open / parse / “understand”.