PHP regex for validating a URL

后端 未结 4 1705
囚心锁ツ
囚心锁ツ 2020-12-02 00:38

I\'m looking for a decent regex to match a URL (a full URL with scheme, domain, path etc.) I would normally use filter_var but I can\'t in this case as I have to support PHP

4条回答
  •  庸人自扰
    2020-12-02 01:21

    You could try this one. I haven't tried it myself but it's surely the biggest regexp I've ever seen, haha.

    ^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$
    

提交回复
热议问题