在 PHP 中验证 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7003416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validating a URL in PHP
提问by Oliver 'Oli' Jensen
Possible Duplicate:
PHP validation/regex for URL
可能的重复:
URL 的 PHP 验证/正则表达式
Is there any easy, secure and fast way to check if a URL is valid in PHP?
有没有简单、安全、快速的方法来检查一个 URL 在 PHP 中是否有效?
回答by Dan Grossman
Yes, there is! Use filter_var
:
就在这里!使用filter_var
:
if (filter_var($url, FILTER_VALIDATE_URL) !== false) ...
FILTER_VALIDATE_URL
validates URLs according to RFC 2396.
FILTER_VALIDATE_URL
根据RFC 2396验证 URL 。
回答by Devin M
Well if we look at RFC 3986we can find the definition of a URL.
好吧,如果我们查看RFC 3986,我们可以找到 URL 的定义。
And if we take a look at Appendix B there is a guide to using regular expressions to parse a URL:
如果我们看一下附录 B,有一个使用正则表达式解析 URL 的指南:
Appendix B. Parsing a URI Reference with a Regular Expression
As the "first-match-wins" algorithm is identical to the "greedy"
disambiguation method used by POSIX regular expressions, it is
natural and commonplace to use a regular expression for parsing the
potential five components of a URI reference.The following line is the regular expression for breaking-down a
well-formed URI reference into its components.^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? 12 3 4 5 6 7 8 9
The numbers in the second line above are only to assist readability; they indicate the reference points for each subexpression (i.e., each paired parenthesis). We refer to the value matched for subexpression as $. For example, matching the above expression to
http://www.ics.uci.edu/pub/ietf/uri/#Related
results in the following subexpression matches:
= http: = http = //www.ics.uci.edu = www.ics.uci.edu = /pub/ietf/uri/ = <undefined> = <undefined> = #Related = Related
where indicates that the component is not present, as is the case for the query component in the above example. Therefore, we can determine the value of the five components as
scheme = authority = path = query = fragment =
Going in the opposite direction, we can recreate a URI reference from its components by using the algorithm of Section 5.3.
附录 B. 使用正则表达式解析 URI 引用
由于“first-match-wins”算法与
POSIX 正则表达式使用的“贪婪”消歧方法相同,因此
使用正则表达式来解析
URI 引用的潜在五个组件是很自然和常见的。以下行是将
格式良好的 URI 引用分解为其组件的正则表达式。^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? 12 3 4 5 6 7 8 9
上面第二行中的数字只是为了便于阅读;它们表示每个子表达式的参考点(即每个成对的括号)。我们将与子表达式匹配的值称为 $。例如,将上面的表达式匹配到
http://www.ics.uci.edu/pub/ietf/uri/#Related
导致以下子表达式匹配:
= http: = http = //www.ics.uci.edu = www.ics.uci.edu = /pub/ietf/uri/ = <undefined> = <undefined> = #Related = Related
where 表示该组件不存在,就像上面例子中的查询组件一样。因此,我们可以确定五个分量的值如下
scheme = authority = path = query = fragment =
相反,我们可以使用 5.3 节的算法从其组件重新创建 URI 引用。
You can ues this regular expression to parse the URL manually or use the built in parse_url functionavalable in PHP 4 and 5
您可以使用此正则表达式手动解析 URL 或使用PHP 4 和 5 中可用的内置parse_url 函数
回答by Code Magician
It depends on your definition of valid. Semantically valid, domain name resolves, etc.
这取决于您对有效的定义。语义有效,域名解析等。
The quick approach would be to use preg_match to test the url against a good regular expression to validate it's of the correct format. There appear to be some good examples on this thread PHP validation/regex for URL
快速的方法是使用 preg_match 根据一个好的正则表达式来测试 url,以验证它的格式是否正确。这个线程上似乎有一些很好的例子,用于 URL 的 PHP 验证/正则表达式