Is FILTER_VALIDATE_URL being too strict?

后端 未结 4 1273
[愿得一人]
[愿得一人] 2021-02-12 16:26

In PHP, filter_var(\'www.example.com\', FILTER_VALIDATE_URL) returns false. Is this correct? Isn\'t www.example.com a valid URL, or protoc

4条回答
  •  囚心锁ツ
    2021-02-12 17:05

    In addition to Paul Dixon's answer I want to say that you can use flags for FILTER_VALIDATE_URL to specify which part of the URL must be presented.

    FILTER_FLAG_SCHEME_REQUIRED
    FILTER_FLAG_HOST_REQUIRED
    FILTER_FLAG_PATH_REQUIRED
    FILTER_FLAG_QUERY_REQUIRED
    

    Since PHP 5.2.1 FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED flags used by default and, unfortunately, there is no way to disable them (you can't do something like filter_var($url, FILTER_VALIDATE_URL, ~FILTER_FLAG_SCHEME_REQUIRED); if the existence of the URL scheme part does not necessarily). It seems like a bug for me. There is a relative bugreport.

提交回复
热议问题