A regular expression to validate URLs.
I made this repository specifically for Mathias Bynens' little “[in search of the perfect URL validation regex] (https://mathiasbynens.be/demo/url-regex)” challenge and if I may spoiler you from the beginning, mine is not perfect. But it is a step closer to it., (*1)
The regular expression contains parts from [Diego Perini's regular expression and the comments on his Gist] (https://gist.github.com/dperini/729294) as well as some stuff from the [Symfony URL constraint pattern] (https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php#L34-L36)., (*2)
Note that the challenge does not cover all possible valid URL constructs. The unit test contains several URLs which should be valid and not valid which are not part of the challenge., (*3)
Also note that this class is not meant as a real validator, it is more a starting point for a validator. I had to release the code under the MIT license because it incorporates a big portion of Diego Perini's regular expression—but I will ask him if it is possible for me to release it under the Unlicense license. If you plan to use this regular expression in your code consider to remove the username, password, port and IP address support; since such addresses should not be used for e.g. homepages of users on a profile page or within comments on a blog post., (*4)
On a last note, the class also contains a scheme (aka protocol) validation regular expression., (*5)
The provided unit test has a 100% coverage of the little class and the code is PHP 5.3+ compatible., (*6)
The class and tests are available via composer., (*7)
composer require fleshgrinder/url-validator dev-master
filter_varimplementation, find a way to validate it with the regular expression.
The MIT License (MIT), (*8)
For more information, please refer to https://en.wikipedia.org/wiki/MIT_License, (*9)