简体   繁体   English

PHP filter_var() - FILTER_VALIDATE_URL

[英]PHP filter_var() - FILTER_VALIDATE_URL

The FILTER_VALIDATE_URL filter seems to have some trouble validating non-ASCII URLs: FILTER_VALIDATE_URL过滤器似乎在验证非ASCII网址时遇到一些问题:

var_dump(filter_var('http://pt.wikipedia.org/wiki/', FILTER_VALIDATE_URL)); // http://pt.wikipedia.org/wiki/
var_dump(filter_var('http://pt.wikipedia.org/wiki/Guimarães', FILTER_VALIDATE_URL)); // false

Why isn't the last URL correctly validated? 为什么没有正确验证最后一个URL? And what are the possible workarounds? 什么是可能的解决方法? Running PHP 5.3.0. 运行PHP 5.3.0。

I'd also like to know where I can find the source code of the FILTER_VALIDATE_URL validation filter. 我还想知道在哪里可以找到FILTER_VALIDATE_URL验证过滤器的源代码。

Technically that is not a valid URL according to section 5 of RFC 1738. Browsers will automatically encode the ã character to %C3%A3 before sending the request to the server. 从技术上讲,根据RFC 1738的第5部分,它不是有效的URL。浏览​​器会在将请求发送到服务器之前自动将ã字符编码为%C3%A3。 The technically valid full url here is: http://pt.wikipedia.org/wiki/Guimar%C3%A3es Pass that to the VALIDATE_URL filter and it will work fine. 这里技术上有效的完整网址是: http//pt.wikipedia.org/wiki/Guimar%C3%A3es将其传递给VALIDATE_URL过滤器,它将正常工作。 The filter only validates according to spec, it doesn't try to fix/encode characters for you. 过滤器仅根据规范验证,它不会尝试为您修复/编码字符。

The parsing starts here: 解析从这里开始:
http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c?view=markup http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c?view=markup

and is actually done in /trunk/ext/standard/url.c 并且实际上是在/trunk/ext/standard/url.c中完成的

At a first glance I can't see anything that purposely rejects non-ASCII characters, so it's probably just lack of unicode support. 乍一看,我看不到任何故意拒绝非ASCII字符的东西,所以它可能只是缺乏unicode支持。 PHP is not good in handling non-ASCII characters anywhere. PHP无法在任何地方处理非ASCII字符。 :( :(

The following code uses filter_var but encode non ascii chars before calling it. 以下代码使用filter_var但在调用之前编码非ascii字符。 Hope this helps someone. 希望这有助于某人。

<?php

function validate_url($url) {
    $path = parse_url($url, PHP_URL_PATH);
    $encoded_path = array_map('urlencode', explode('/', $path));
    $url = str_replace($path, implode('/', $encoded_path), $url);

    return filter_var($url, FILTER_VALIDATE_URL) ? true : false;
}

// example
if(!validate_url("http://somedomain.com/some/path/file1.jpg")) {
    echo "NOT A URL";
}
else {
    echo "IS A URL";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM