任何以 http://开头的内容都由 FILTER_VALIDATE_URL 验证? [英] Anything start with http:// is validated by FILTER_VALIDATE_URL?
问题描述
我用我可以想象的字符串和整数进行了测试,只要它以 http://开头,它将是使用 FILTER_VALIDATE_URL 的有效 url.那么,为什么我们需要 FILTER_VALIDATE_URL?当我们想让它有效时,为什么不直接在输入上添加 http://?
I tested with strings and int that I can imagine, as long as it start with http://, it will be a valid url using FILTER_VALIDATE_URL. So, why we need FILTER_VALIDATE_URL? Why not just add http:// on an input whenever we want to make it valid?
var_dump(filter_var ('http://example',FILTER_VALIDATE_URL ));
推荐答案
从技术上讲,任何以方案(如 http://
)开头并在其后包含有效 URI 字符的 URI 有效,根据 RFC 3986 中的官方 URI 规范:
Well technically, any URI that starts with a scheme (like http://
) and contains valid URI characters after that is valid as per the official URI specification in RFC 3986:
每个 URI 都以一个方案名称开头,如第 3.1 节中所定义,它指的是在该方案中分配标识符的规范.因此,URI 语法是一个联合且可扩展的命名系统,其中每个方案的规范可能会进一步限制使用该方案的标识符的语法和语义.
Each URI begins with a scheme name, as defined in Section 3.1, that refers to a specification for assigning identifiers within that scheme. As such, the URI syntax is a federated and extensible naming system wherein each scheme's specification may further restrict the syntax and semantics of identifiers using that scheme.
因此,您获得的回报并不奇怪——这就是应该发生的事情.至于为什么你应该使用带有 FILTER_VALIDATE_URL
标志的 filter_var
......它在语义上比对每个可能的 URL 方案做类似下面的事情更合适,不是吗?同意吗?
So there's nothing strange about the return you're getting -- that's what's supposed to happen. As to why you should use the filter_var
with the FILTER_VALIDATE_URL
flag ... it's way more semantically appropriate than doing something like the following for every possible URL scheme, wouldn't you agree?
if (strpos($url, 'http://') === 0
|| strpos($url, 'ftp://') === 0
|| strpos($url, 'telnet://') === 0
) {
// it's a valid URL!
}
这篇关于任何以 http://开头的内容都由 FILTER_VALIDATE_URL 验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!