php URL 是否有效

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

is URL valid or not

phpurlurl-validation

提问by james

Possible Duplicate:
what is the best way to check if a Url exists in PHP ?`

可能的重复:
检查 PHP 中是否存在 Url 的最佳方法是什么?`

I'm looking for a function that returns TRUE or FALSE in php, either the URL is valid or not.

我正在寻找一个在 php 中返回 TRUE 或 FALSE 的函数,无论 URL 是否有效。

isValidURL($url); i think that simple... That would take into count all kind of URL possible.

isValidURL($url); 我认为这很简单...这将考虑所有可能的 URL。

By valid i want It to referes to an existing page of the web or other kind of files. It just should exist

通过有效,我希望它引用网络的现有页面或其他类型的文件。它应该存在

回答by genesis

<?php

$url = "http://stack*overflow.org";


if(filter_var($url, FILTER_VALIDATE_URL) === FALSE)
{
        echo "Not valid";
}else{
        echo "VALID";
}
?>

this does not check tldsthough

虽然这不会检查tlds

回答by Nemoden

You can check whether URL is valid or not using parse_urlfunction which would return falseif URL is not valid and an arrayotherwise.

您可以使用parse_url函数检查 URL 是否有效,false如果 URL 无效则返回,array否则返回。

function isValidURL($url) { return (bool)parse_url($url); }

pretty easy way, huh? :)

很简单的方法吧?:)