2017 © Pedro Peláez
 

library url-validator

A regular expression to validate URLs.

image

fleshgrinder/url-validator

A regular expression to validate URLs.

  • Tuesday, March 15, 2016
  • by Fleshgrinder
  • Repository
  • 1 Watchers
  • 6 Stars
  • 6,717 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 1 Open issues
  • 1 Versions
  • 11 % Grown

The README.md

URL Validation

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)

Features

  • Full Internationalized Domain Name (IDN) support.
  • Full support for Punycode.
  • Support for IPv4 and IPv6 addresses as hostname.
  • Extraction of URL parts (like parse_url):
    • Scheme (aka protocol)
    • Username
    • Password
    • Hostname
    • Domain + TLD
    • IPv4
    • IPv6
    • Port
    • Path
    • Query
    • Fragment

Install

The class and tests are available via composer., (*7)

composer require fleshgrinder/url-validator dev-master

TODO

  • IPv6 address validation totally relies on PHP's filter_var implementation, find a way to validate it with the regular expression.
  • Port the regular expression to JavaScript for usage in HTML input URL elements and of course JavaScript itself.
  • Find more funny URLs for the unit test.

License

The MIT License (MIT), (*8)

For more information, please refer to https://en.wikipedia.org/wiki/MIT_License, (*9)

The Versions

15/03 2016

dev-master

9999999-dev

A regular expression to validate URLs.

  Sources   Download

Unlicense

The Requires

  • php >=5.3
  • ext-intl *

 

The Development Requires

url validation uri regex regexp regular expression