Validate URL in PHP without regex
In PHP we have had a default URL validation like this:
// truthy if we have valid URL
filter_var($url, FILTER_VALIDATE_URL);
But this approach would fail for working URLs with utf-8 symbols. Consider this website: http://www.medizinische-übersetzungen.biz/
Validation of the example from above will fail because `filter_var` implementation is not aware of multibyte encodings.
A safer approach to validate URL you’re receiving from users:
// truthy if we have valid URL
parse_url($url, PHP_URL_SCHEME) && parse_url($url, PHP_URL_HOST);
There is a drawback in the suggested solution: parse_url
won’t catch strange characters in URL:
input: http://www.ex=ample.com
filter_var: FAIL
parse_url: PASS // the only failure of suggested solution
But from experience, I usually see a missing schema or completely invalid values in the form field, this solution is good enough.
Check test cases in https://github.com/terales/php-url-validation-example