peterpostmann/parse_uri

解析URI并返回其组件

1.0.0 2017-11-17 15:27 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:31:25 UTC


README

Software License Build Status

解析URI并返回其组件

此函数解析URI(RFC3986 URL,URN或Windows路径)并返回一个关联数组,包含URL的任意组件。数组元素的值没有进行URL解码。

此函数的目的是分解给定的URL,而不是验证URL。

安装

通过Composer

composer require peterpostmann/parse_uri

如果您不想使用Composer,只需复制parse_uri.php文件并将其包含到您的项目中。

为什么

此函数扩展了parse_url的功能。它解析rfc兼容的URL和URN以及(Windows)文件路径(基本上是所有可以传递给文件函数的内容(例如fopen,file_get_contents))。

用法

use function peterpostmann\uri\parse_uri;

array parse_uri ( string uri [, int $component = -2 [, bool $convertUrlToUrn = null ]]) 

通过选项,输出可以减少到parse_url的输出。否则将提供额外的组件。

URL

schema://user:pass@host:port/path?query#fragment

array (size=14)
  'scheme' => string 'schema' (length=6)
  'host' => string 'host' (length=4)
  'port' => string '' (length=0)
  'user' => string 'user' (length=4)
  'pass' => string 'pass' (length=4)
  'path' => string 'port/path' (length=9)
  'query' => string 'query' (length=5)
  'fragment' => string 'fragment' (length=8)
  '_protocol' => string 'schema' (length=6)
  '_userinfo' => string 'user:pass@' (length=10)
  '_authority' => string 'user:pass@host:' (length=15)
  '_document' => string 'schema://user:pass@host:port/path' (length=33)
  '_ressource' => string 'schema://user:pass@host:port/path?query' (length=39)
  '_uri' => string 'schema://user:pass@host:port/path?query#fragment' (length=48)

URI

schema:path?query#fragment

array (size=8)
  'scheme' => string 'schema' (length=6)
  'path' => string 'path' (length=4)
  'query' => string 'query' (length=5)
  'fragment' => string 'fragment' (length=8)
  '_protocol' => string 'schema' (length=6)
  '_document' => string 'schema:path' (length=11)
  '_ressource' => string 'schema:path?query' (length=17)
  '_uri' => string 'schema:path?query#fragment' (length=26)

_protocol将返回

  • {schema}如果存在
  • 'file'如果它是绝对路径('path')
  • false如果它是相对路径('path/file', 'file')
  • true如果它是Windows路径('C:\path')
  • null如果不存在路径

示例

解析URI

use function peterpostmann\uri\parse_uri;

echo "# URIs (with standard components)\n\n";

var_dump(parse_uri('/path/to/file.ext', peterpostmann\uri\PARSE_URI_DEFAULT));
var_dump(parse_uri('relative/path/to/file.ext', peterpostmann\uri\PARSE_URI_DEFAULT));
var_dump(parse_uri('fileInCwd.ext', peterpostmann\uri\PARSE_URI_DEFAULT));
var_dump(parse_uri('C:/path/to/winfile.ext', peterpostmann\uri\PARSE_URI_DEFAULT));
var_dump(parse_uri('C:\path\to\winfile.ext', peterpostmann\uri\PARSE_URI_DEFAULT));
var_dump(parse_uri('\\\\smbserver\share\path\to\winfile.ext', peterpostmann\uri\PARSE_URI_DEFAULT));
var_dump(parse_uri('file:///path/to/file.ext', peterpostmann\uri\PARSE_URI_DEFAULT));
var_dump(parse_uri('http://user:pass@example.org:8888/path/to/file', peterpostmann\uri\PARSE_URI_DEFAULT));
var_dump(parse_uri('news:comp.infosystems.www.servers.unix', peterpostmann\uri\PARSE_URI_DEFAULT));

echo "# URIs (with additional components)\n\n";

var_dump(parse_uri('C:\path\to\winfile.ext'));
var_dump(parse_uri('\\\\smbserver\share\path\to\winfile.ext'));
var_dump(parse_uri('file:///path/to/file.ext'));
var_dump(parse_uri('http://user:pass@example.org:8888/path/to/file'));
var_dump(parse_uri('news:comp.infosystems.www.servers.unix'));

上面的示例将输出

# URIs (with standard components)

array (size=1)
  'path' => string '/path/to/file.ext' (length=17)
 
array (size=1)
  'path' => string 'relative/path/to/file.ext' (length=25)

array (size=1)
  'path' => string 'fileInCwd.ext' (length=13)

array (size=1)
  'path' => string 'C:\path\to\winfile.ext' (length=22)

array (size=1)
  'path' => string 'C:\path\to\winfile.ext' (length=22)

array (size=3)
  'scheme' => string 'file' (length=4)
  'host' => string 'smbserver' (length=9)
  'path' => string '/share/path/to/winfile.ext' (length=26)

array (size=3)
  'scheme' => string 'file' (length=4)
  'host' => string '' (length=0)
  'path' => string '/path/to/file.ext' (length=17)

array (size=6)
  'scheme' => string 'http' (length=4)
  'host' => string 'example.org' (length=11)
  'port' => int 8888
  'user' => string 'user' (length=4)
  'pass' => string 'pass' (length=4)
  'path' => string '/path/to/file' (length=13)

array (size=2)
  'scheme' => string 'news' (length=4)
  'path' => string 'comp.infosystems.www.servers.unix' (length=33)

# URIs (with additional components)

array (size=5)
  'path' => string 'C:\path\to\winfile.ext' (length=22)
  '_protocol' => string 'file' (length=4)
  '_document' => string 'C:\path\to\winfile.ext' (length=22)
  '_ressource' => string 'C:\path\to\winfile.ext' (length=22)
  '_uri' => string 'C:\path\to\winfile.ext' (length=22)

array (size=8)
  'scheme' => string 'file' (length=4)
  'host' => string 'smbserver' (length=9)
  'path' => string '/share/path/to/winfile.ext' (length=26)
  '_protocol' => string 'file' (length=4)
  '_authority' => string 'smbserver' (length=9)
  '_document' => string '/share/path/to/winfile.ext' (length=26)
  '_ressource' => string '/share/path/to/winfile.ext' (length=26)
  '_uri' => string '/share/path/to/winfile.ext' (length=26)

array (size=7)
  'scheme' => string 'file' (length=4)
  'host' => string '' (length=0)
  'path' => string '/path/to/file.ext' (length=17)
  '_protocol' => string 'file' (length=4)
  '_document' => string 'file:///path/to/file.ext' (length=24)
  '_ressource' => string 'file:///path/to/file.ext' (length=24)
  '_uri' => string 'file:///path/to/file.ext' (length=24)

array (size=12)
  'scheme' => string 'http' (length=4)
  'host' => string 'example.org' (length=11)
  'port' => int 8888
  'user' => string 'user' (length=4)
  'pass' => string 'pass' (length=4)
  'path' => string '/path/to/file' (length=13)
  '_protocol' => string 'http' (length=4)
  '_userinfo' => string 'user:pass@' (length=10)
  '_authority' => string 'user:pass@example.org:8888' (length=26)
  '_document' => string 'http://user:pass@example.org:8888/path/to/file' (length=46)
  '_ressource' => string 'http://user:pass@example.org:8888/path/to/file' (length=46)
  '_uri' => string 'http://user:pass@example.org:8888/path/to/file' (length=46)

array (size=6)
  'scheme' => string 'news' (length=4)
  'path' => string 'comp.infosystems.www.servers.unix' (length=33)
  '_protocol' => string 'news' (length=4)
  '_document' => string 'news:comp.infosystems.www.servers.unix' (length=38)
  '_ressource' => string 'news:comp.infosystems.www.servers.unix' (length=38)
  '_uri' => string 'news:comp.infosystems.www.servers.unix' (length=38)

patch URI

use function peterpostmann\uri\parse_uri;
use function peterpostmann\uri\build_uri;

$uri = 'https://example.org/path/to/file?query#fragment':
$patch = [ 'path' => '/path/to/otherfile']
echo build_uri($patch + parse_uri($uri));

上面的示例将输出

https://example.org/path/to/otherfile?query#fragment

加载引用

function resolveReference($reference)
{
    $components = parse_uri($reference);

    if(!isset($components['fragment']))
        return null;

    // absolute reference
    if(isset($components['_document'])) {
        $document = $this->getLoader($components['_protocol'])->load($components['_document']);
    } else { // relative reference
        $document = $this->currentDocument();
    }

    return $this->getReference($document, $components['fragment']);
}

$components['_document']返回不带片段的URI。($components['_ressource']不带片段和查询)。$components['_protocol']表示协议。它通常与schema相同,除了file:/// URI。对于绝对、相对和Windows路径,schema未设置,但$components['_protocol']设置为file。对于没有schema的URI \\example.org\path变量已设置,但为空。

辅助函数

build_uri

该函数从其组件创建URI(字符串)。

use function peterpostmann\uri\build_uri;

echo build_uri([
          'scheme' => 'ssh2.sftp',
          'host' => 'example.com',
          'port' => 422,
          'user' => 'user',
          'pass' => 'pass',
          'path' => '/file.php',
          'query' => 'var1=val1&var2=val2',
          'fragment' => 'anchor'
      ])."\n";
      
echo build_uri([
          'scheme' => 'news',
          'path' => 'comp.infosystems.www.servers.unix',
      ])."\n";

上面的示例将输出

ssh2.sftp://user:pass@example.com:422/file.php?var1=val1&var2=val2#anchor
news:comp.infosystems.www.servers.unix

convert_url2urn

该函数将PHP URL转换为URN(它们最初应该是这样的)。

use function peterpostmann\uri\build_uri;

echo convert_url2urn('data://text/plain;base64,SSBsb3ZlIFBIUAo=')."\n";
echo convert_url2urn('zlib://archive.zip#dir/file.txt')."\n";
echo convert_url2urn('php://stdin')."\n";

上面的示例将输出

data:text/plain;base64,SSBsb3ZlIFBIUAo='
zlib:archive.zip#dir/file.txt
php:stdin

如果按原样解析URI,解析器将寻找不存在的宿主部分。convert_url2urn接受第二个参数来控制转换

  • null: 不转换,除非格式来自https://php.ac.cn/manual/en/wrappers.php(php://, compress.*://, zip://, zlib://, bzip2://, data://, glob://, phar://, rar://, ogg://, expect://)
  • true: 转换所有
  • false: 不转换任何内容

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件