Checking for relative vs absolute paths/URLs in PHP

前端 未结 7 637
既然无缘
既然无缘 2021-01-17 15:45

I need to implement functions to check whether paths and urls are relative, absolute, or invalid (invalid syntactically- not whether resource exists). What are the range of

7条回答
  •  渐次进展
    2021-01-17 16:15

    I've recently started a composer package that might be useful for checking wether URL's are relative / absolute (and more, ofcourse).

    Check out the repository here: https://github.com/Enrise/UriHelper Or the composer Packagists package here: https://packagist.org/packages/enrise/urihelper

    Some examples:

    $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
    

提交回复
热议问题