12

我需要实现函数来检查路径和 url 是相对的、绝对的还是无效的(在语法上无效 - 不是资源是否存在)。我应该寻找哪些案例?

function check_path($dirOrFile) {
    // If it's an absolute path: (Anything that starts with a '/'?)
        return 'absolute';
    // If it's a relative path: 
        return 'relative';
    // If it's an invalid path:
        return 'invalid';
}

function check_url($url) {
    // If it's an absolute url: (Anything that starts with a 'http://' or 'https://'?)
        return 'absolute';
    // If it's a relative url:
        return 'relative';
    // If it's an invalid url:
        return 'invalid';
}
4

7 回答 7

5

绝对路径和 URL

您是对的,Linux 中的绝对 URL 必须以 开头/,因此检查路径开头的斜杠就足够了。

对于 URL,您需要检查http://https://,正如您所写,但是,还有更多以ftp://,sftp://或开头的 URL smb://。因此,这在很大程度上取决于您要涵盖的用途范围。

无效的路径和 URL

假设您指的是 Linux,路径中唯一被禁止的字符是/and \0。这实际上非常依赖于文件系统,但是,您可以假设上述内容对于大多数用途都是正确的。

在 Windows 中,它更复杂。您可以在备注下的Path.GetInvalidPathChars 方法文档中了解它。

URL 比 Linux 路径更复杂,因为唯一允许的字符是A-Z, a-z, 0-9, -, ., _, ~, :, /, ?, #, [, ], @, !, $, &, ', (, ), *, +, ,,;和(如此=的另一个答案中所述)。

相对路径和 URL

通常,既不是绝对也不是无效的路径和 URL 是相对的。

于 2011-09-12T19:13:43.670 回答
3

这个函数取自 Drupal

public function is_absolute($url)
{
    $pattern = "/^(?:ftp|https?|feed):\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*
    (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@)?(?:
    (?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?]
    (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/xi";

    return (bool) preg_match($pattern, $url);
}
于 2016-09-25T00:47:22.883 回答
3

如果您已经知道 URL 格式正确

if(strpos($uri,'://')!==false){
    //protocol: absolute url
}elseif(substr($uri,0,1)=='/'){
    //leading '/': absolute to domain name (half relative)
}else{
    //no protocol and no leading slash: relative to this page
}
于 2019-07-25T10:39:21.067 回答
3

由于声誉不佳,我无法对答案发表评论,因此我必须使用他从 Drupal 库中复制的函数来回复 ymakux 答案。

我正在使用此功能,我发现带有查询部分的网址(?符号后的文本)包含 | 符号将被评估为假

例如:

https://example.com/image.jpeg?fl=res,749,562,3|shr,,20|jpg,90

将被评估为假。

您所要做的就是添加

\|

查询正则表达式的一部分,使函数看起来像:

public static function isAbsoluteUrl($url)
    {
        $pattern = "/^(?:ftp|https?|feed)?:?\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@)?(?:
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?]
        (?:[\w#!:\.\?\+\|=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/xi";

        return (bool) preg_match($pattern, $url);
    }

希望它可以帮助某人:)

于 2017-07-19T09:50:27.993 回答
2

我最近启动了一个 composer 包,它可能有助于检查 URL 是否是相对/绝对的(当然还有更多)。

在此处查看存储库:https ://github.com/Enrise/UriHelper 或在此处查看 composer Packagists 包:https ://packagist.org/packages/enrise/urihelper

一些例子:

$uri = new \Enrise\Uri('http://usr:pss@example.com:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment');
echo $uri->getScheme(); // http
echo $uri->getUser(); // usr
echo $uri->getPass(); // pss
echo $uri->getHost(); // example.com
echo $uri->getPort(); // 81
echo $uri->getPath(); // /mypath/myfile.html
echo $uri->getQuery(); // a=b&b[]=2&b[]=3
echo $uri->getFragment(); // myfragment
echo $uri->isSchemeless(); // false
echo $uri->isRelative(); // false

$uri->setScheme('scheme:child:scheme.VALIDscheme123:');
$uri->setPort(null);

echo $uri->getUri(); //scheme:child:scheme.VALIDscheme123:usr:pss@example.com/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment
于 2015-04-21T09:12:46.393 回答
2

利用:

function isAbsolute($url) {
  return isset(parse_url($url)['host']);
}

解释:

如果设置了主机,则路径是绝对的。

例如:

$test = [
'/link?param=1'=>parse_url('/assa?ass'),
'//aaa.com/link?param=1'=>parse_url('//assa?ass'),
'http://aaa.com/link?param=1'=>parse_url('http://as.plassa?ass')
];
var_export($test);

/* Output:
[
  "/link?param=1" => array:2 [▼ // Not absolute
    "path" => "/assa"
    "query" => "ass"
  ]
  "//aaa.com/link?param=1" => array:2 [▼ // Absolute because of host
    "host" => "assa"
    "query" => "ass"
  ]
  "http://aaa.com/link?param=1" => array:3 [▼ // Absolute because of host
    "scheme" => "http"
    "host" => "as.plassa"
    "query" => "ass"
  ]
]
*/
于 2020-09-30T09:54:41.220 回答
2

Symfony FileSystem 组件检查路径是否为绝对路径:

public function isAbsolutePath($file)
{
    return strspn($file, '/\\', 0, 1)
        || (strlen($file) > 3 && ctype_alpha($file[0])
            && substr($file, 1, 1) === ':'
            && strspn($file, '/\\', 2, 1)
        )
        || null !== parse_url($file, PHP_URL_SCHEME)
    ;
}
于 2016-11-09T17:42:27.357 回答