PHP regex for url validation, filter_var is too permisive

needaboloumma

New Member
First lets define a "URL" according to my requirements.The only protocols optionally allowed are \[code\]http://\[/code\] and \[code\]https://\[/code\]then a mandatory domain name like \[code\]stackoverflow.com\[/code\]then optionally the rest of url components (\[code\]path\[/code\], \[code\]query\[/code\], \[code\]hash\[/code\], ...)For reference a list of valid and invalid url's according to my requirementsVALIDINVALID
  • http://www (php filter_var allow this, yes i know is a \[code\]valid\[/code\] url)
  • google
  • http://www..des (php filter_var allow this)
  • Any url with not allowed characters in the domain name
For completeness here is my php version: \[code\]5.3.2-1ubuntu4.2\[/code\]
 
Back
Top