php 检查 URL 是否有效的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2058578/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:50:44  来源:igfitidea点击:

Best way to check if a URL is valid

php

提问by Ryan

I want to use PHP to check, if string stored in $myoutputvariable contains a valid link syntax or is it just a normal text. The function or solution, that I'm looking for, should recognize all links formats including the ones with GET parameters.

我想使用 PHP 来检查存储在$myoutput变量中的字符串是否包含有效的链接语法或者它只是一个普通文本。我正在寻找的功能或解决方案应该识别所有链接格式,包括带有 GET 参数的链接格式。

A solution, suggested on many sites, to actually query string (using CURL or file_get_contents()function) is not possible in my case and I would like to avoid it.

在许多站点上建议的实际查询字符串(使用 CURL 或file_get_contents()函数)的解决方案在我的情况下是不可能的,我想避免它。

I thought about regular expressions or another solution.

我想到了正则表达式或其他解决方案。

回答by Gordon

You can use a native Filter Validator

您可以使用本机过滤器验证器

filter_var($url, FILTER_VALIDATE_URL);

Validates value as URL (according to ? http://www.faqs.org/rfcs/rfc2396), optionally with required components. Beware a valid URL may not specify the HTTP protocol http:// so further validation may be required to determine the URL uses an expected protocol, e.g. ssh:// or mailto:. Note that the function will only find ASCII URLs to be valid; internationalized domain names (containing non-ASCII characters) will fail.

将值验证为 URL(根据 ? http://www.faqs.org/rfcs/rfc2396),可选择使用所需的组件。请注意,有效的 URL 可能不会指定 HTTP 协议 http://,因此可能需要进一步验证以确定 URL 使用预期的协议,例如 ssh:// 或 mailto:。请注意,该函数只会找到有效的 ASCII URL;国际化域名(包含非 ASCII 字符)将失败。

Example:

例子:

if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
    die('Not a valid URL');
}

回答by Erich García

Here is the best tutorial I found over there:

这是我在那里找到的最好的教程:

http://www.w3schools.com/php/filter_validate_url.asp

http://www.w3schools.com/php/filter_validate_url.asp

<?php
$url = "http://www.qbaki.com";

// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);

// Validate url
if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
echo("$url is a valid URL");
} else {
echo("$url is not a valid URL");
}
?>

Possible flags:

可能的标志:

FILTER_FLAG_SCHEME_REQUIRED - URL must be RFC compliant (like http://example)
FILTER_FLAG_HOST_REQUIRED - URL must include host name (like http://www.example.com)
FILTER_FLAG_PATH_REQUIRED - URL must have a path after the domain name (like www.example.com/example1/)
FILTER_FLAG_QUERY_REQUIRED - URL must have a query string (like "example.php?name=Peter&age=37")

回答by Huey Ly

Using filter_var() will fail for urls with non-ascii chars, e.g. (http://pt.wikipedia.org/wiki/Guimar?es). The following function encode all non-ascii chars (e.g. http://pt.wikipedia.org/wiki/Guimar%C3%A3es) before calling filter_var().

对于带有非 ascii 字符的 URL,使用 filter_var() 将失败,例如(http://pt.wikipedia.org/wiki/Guimar?es)。以下函数在调用 filter_var() 之前对所有非 ascii 字符(例如http://pt.wikipedia.org/wiki/Guimar%C3%A3es)进行编码。

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";
}

回答by mghhgm

function is_url($uri){
    if(preg_match( '/^(http|https):\/\/[a-z0-9_]+([\-\.]{1}[a-z_0-9]+)*\.[_a-z]{2,5}'.'((:[0-9]{1,5})?\/.*)?$/i' ,$uri)){
      return $uri;
    }
    else{
        return false;
    }
}

回答by Md. Noor-A-Alam Siddique

Personally I would like to use regular expression here. Bellow code perfectly worked for me.

我个人想在这里使用正则表达式。波纹管代码非常适合我。

$baseUrl     = url('/'); // for my case https://www.xrepeater.com
$posted_url  = "home";
// Test with one by one
/*$posted_url  = "/home";
$posted_url  = "xrepeater.com";
$posted_url  = "www.xrepeater.com";
$posted_url  = "http://www.xrepeater.com";
$posted_url  = "https://www.xrepeater.com";
$posted_url  = "https://xrepeater.com/services";
$posted_url  = "xrepeater.dev/home/test";
$posted_url  = "home/test";*/

$regularExpression  = "((https?|ftp)\:\/\/)?"; // SCHEME Check
$regularExpression .= "([a-z0-9+!*(),;?&=$_.-]+(\:[a-z0-9+!*(),;?&=$_.-]+)?@)?"; // User and Pass Check
$regularExpression .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP Check
$regularExpression .= "(\:[0-9]{2,5})?"; // Port Check
$regularExpression .= "(\/([a-z0-9+$_-]\.?)+)*\/?"; // Path Check
$regularExpression .= "(\?[a-z+&$_.-][a-z0-9;:@&%=+\/$_.-]*)?"; // GET Query String Check
$regularExpression .= "(#[a-z_.-][a-z0-9+$_.-]*)?"; // Anchor Check

if(preg_match("/^$regularExpression$/i", $posted_url)) { 
    if(preg_match("@^http|https://@i",$posted_url)) {
        $final_url = preg_replace("@(http://)+@i",'http://',$posted_url);
        // return "*** - ***Match : ".$final_url;
    }
    else { 
          $final_url = 'http://'.$posted_url;
          // return "*** / ***Match : ".$final_url;
         }
    }
else {
     if (substr($posted_url, 0, 1) === '/') { 
         // return "*** / ***Not Match :".$final_url."<br>".$baseUrl.$posted_url;
         $final_url = $baseUrl.$posted_url;
     }
     else { 
         // return "*** - ***Not Match :".$posted_url."<br>".$baseUrl."/".$posted_url;
         $final_url = $baseUrl."/".$final_url; }
}

回答by Autumn Leonard

Given issues with filter_var() needing http://, I use:

鉴于 filter_var() 需要 http:// 的问题,我使用:

$is_url = filter_var($filename, FILTER_VALIDATE_URL) || array_key_exists('scheme', parse_url($filename));

$is_url = filter_var($filename, FILTER_VALIDATE_URL) || array_key_exists('scheme', parse_url($filename));

回答by Hasan Veli Soyalan

You can use this function, but its will return false if website offline.

您可以使用此功能,但如果网站离线,它会返回false。

  function isValidUrl($url) {
    $url = parse_url($url);
    if (!isset($url["host"])) return false;
    return !(gethostbyname($url["host"]) == $url["host"]);
}

回答by Hayden Frobenius

Actually... filter_var($url, FILTER_VALIDATE_URL); doesn't work very well. When you type in a real url, it works but, it only checks for http:// so if you type something like "http://weirtgcyaurbatc", it will still say it's real.

其实... filter_var($url, FILTER_VALIDATE_URL); 效果不佳。当您输入真实的 url 时,它可以工作,但是,它只检查 http://,因此如果您输入类似“ http://weirtgcyaurbatc”的内容,它仍然会说它是真实的。

回答by Bud Damyanov

Another way to check if given URL is valid is to try to access it, below function will fetch the headers from given URL, this will ensure that URL is valid ANDweb server is alive:

检查给定 URL 是否有效的另一种方法是尝试访问它,下面的函数将从给定 URL 获取标头,这将确保 URL 有效并且Web 服务器处于活动状态:

function is_url($url){
        $response = array();
        //Check if URL is empty
        if(!empty($url)) {
            $response = get_headers($url);
        }
        return (bool)in_array("HTTP/1.1 200 OK", $response, true);
/*Array
(
    [0] => HTTP/1.1 200 OK 
    [Date] => Sat, 29 May 2004 12:28:14 GMT
    [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
    [ETag] => "3f80f-1b6-3e1cb03b"
    [Accept-Ranges] => bytes
    [Content-Length] => 438
    [Connection] => close
    [Content-Type] => text/html
)*/ 
    }   

回答by DaveyJake

Came across this articlefrom 2012. It takes into account variables that may or may notbe just plain URLs.

在2012 年看到这篇文章。它考虑了可能只是也可能不只是普通 URL 的变量。

The author of the article, David Müeller, provides this function that he says, "...could be worth wile [sic]," along with some examples of filter_varand its shortcomings.

这篇文章的作者David Müeller提供了这个函数,他说“......可能值得 [原文如此]”,以及一些例子filter_var及其缺点。

/**
 * Modified version of `filter_var`.
 *
 * @param  mixed $url Could be a URL or possibly much more.
 * @return bool
 */
function validate_url( $url ) {
    $url = trim( $url );

    return (
        ( strpos( $url, 'http://' ) === 0 || strpos( $url, 'https://' ) === 0 ) &&
        filter_var(
            $url,
            FILTER_VALIDATE_URL,
            FILTER_FLAG_SCHEME_REQUIRED || FILTER_FLAG_HOST_REQUIRED
        ) !== false
    );
}