DEV Community

Discussion on: How To Validate URL In PHP With Regex

Collapse
 
moopet profile image
Ben Sinclair • Edited

There are many more common schemes than http, https and ftp, though - and anyone can register their own on your machine if they want.

This will validate using your regex: https://foo.bar:99999 even though it uses an invalid port, while mailto schemes will only work some of the time (depending on where you use colons and so on, and the need to validate an email address being super complicated). The fact that mailto will work at all is a side-effect of your regex thinking it's a username/password section rather than a scheme, and I expect there are combinations which will fail.

I agree that this will get most cases you're likely to care about, and it's probably better than whatever PHP does with FILTER_VALIDATE_URL, but it's never going to be exhaustive unless you write a URL parser.