Search code examples
phpurl-validation

php url validator shows valid address as not valid


if (!filter_var($site, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED))...

This validator shows:
https://www.g - as valid address !
www.google.com - as invalid address !

If you type some address into address bar, and you see that it works - it should be a valid address. And vice versa. I need such one validator. Or - I'm missing something in the above code ?


Solution

  • A user comment on php.net states:

    "Please note that FILTER_FLAG_HOST_REQUIRED and FILTER_FLAG_SCHEME_REQUIRED have disappeared. They were previously mentioned in the constants page but it seems that host and scheme are now required by default for the validate_url filter."

    Seems like the protocol is a must with recent PHP versions and cannot be disabled.