Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP验证URL_Php_Url - Fatal编程技术网

使用PHP验证URL

使用PHP验证URL,php,url,Php,Url,我需要检查表单字段是否是有效的URL(可以通过HTML5完成,但不是每个浏览器都有),我找不到一个好的URL检查功能。这很奇怪,因为通过电子邮件检查,有很多已经完成的功能 我看到(我刚刚发现)了filter\u var()。 我可以找到一些正则表达式,但它们似乎不是很详尽 你知道什么好的url检查功能吗?这是因为url格式的用途非常广泛: Http// is optionnal www is optionnal .com, .net, .us, etc so many possible patt

我需要检查表单字段是否是有效的URL(可以通过HTML5完成,但不是每个浏览器都有),我找不到一个好的URL检查功能。这很奇怪,因为通过电子邮件检查,有很多已经完成的功能

我看到(我刚刚发现)了
filter\u var()。
我可以找到一些正则表达式,但它们似乎不是很详尽


你知道什么好的url检查功能吗?

这是因为url格式的用途非常广泛:

Http// is optionnal
www is optionnal
.com, .net, .us, etc so many possible pattern
.html, /, .php, or nothing are possible ends
与其使用预先制作的函数,我建议您根据大多数用户发送URL的方式、结尾等构建自己的函数


我的两分钱。

让我们稍微提前一点:

<?php 
function validate_url($url) {
  return (bool)preg_match("
      /^                                                      # Start at the beginning of the text
      (?:ftp|https?|feed):\/\/                                # Look for ftp, http, https or feed schemes
      (?:                                                     # Userinfo (optional) which is typically
        (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*      # a username or a username and password
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@          # combination
      )?
      (?:
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+                        # A domain name or a IPv4 address
        |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\])         # or a well formed IPv6 address
      )
      (?::[0-9]+)?                                            # Server port number (optional)
      (?:[\/|\?]
        (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})   # The path and query (optional)
      *)?
    $/xi", $url);
}
?>

或者尝试另一种方法

为什么不使用
文件获取内容($url)
?这样,您还可以知道URL是否有效。 我想你可以使用类似于
fread(fopen($url,'r'),100)的东西所以它不能被注入一些大文件。检查诸如localhost之类的内容仍然是您可能想要做的事情。

只有在PHP 5.2.13或更早版本中才发现filter_var()的问题,如果您使用的是较新版本的PHP,那么我建议使用filter_var()
validate_url($url); // this will return TRUE if the URL is correct. Protocol should exist in $url