检查PHP中的相对vs绝对路径/ URL

时间:2011-09-12 18:33:46

标签: php url relative-path absolute-path relative-url

我需要实现函数来检查路径和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';
}

7 个答案:

答案 0 :(得分:4)

绝对路径和网址

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

对于您需要检查http://https://的网址,但是,有更多网址以ftp://sftp://或{{1}开头}。因此,这取决于您想要涵盖的用途范围。

无效的路径和网址

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

在Windows中它更复杂。您可以在备注下的Path.GetInvalidPathChars Method文档中阅读相关内容。

网址比Linux路径更复杂,因为唯一允许的字符为\0A-Za-z0-9,{{1 }},-._~:/?#[]@!$&'(,{ {1}},)*+(如另一个回答here中所述)。

相对路径和网址

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

答案 1 :(得分:3)

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

我正在使用这个功能,我发现了包含|的查询部分(文字后面的符号)的网址符号将被评估为false

例如:

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

希望它可以帮助某人:)

答案 2 :(得分:2)

我最近创办了一个作曲家套餐,可能对查看其相对/绝对(以及更多,当然)的网址非常有用。

在此处查看存储库:https://github.com/Enrise/UriHelper 或者作曲家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

答案 3 :(得分:1)

此功能取自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);
}

答案 4 :(得分:1)

Symfony FileSystem component检查路径是否是绝对的:

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

答案 5 :(得分:0)

如果您已经知道该网址格式正确

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
}

答案 6 :(得分:0)

我认为最好的是

$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')
];