PHP validation/regex for URL

后端 未结 21 2104
青春惊慌失措
青春惊慌失措 2020-11-22 01:19

I\'ve been looking for a simple regex for URLs, does anybody have one handy that works well? I didn\'t find one with the zend framework validation classes and have seen sev

21条回答
  •  自闭症患者
    2020-11-22 01:51

    Use the filter_var() function to validate whether a string is URL or not:

    var_dump(filter_var('example.com', FILTER_VALIDATE_URL));
    

    It is bad practice to use regular expressions when not necessary.

    EDIT: Be careful, this solution is not unicode-safe and not XSS-safe. If you need a complex validation, maybe it's better to look somewhere else.

提交回复
热议问题