Download the PHP package peterpostmann/parse_uri without Composer
On this page you can find all versions of the php package peterpostmann/parse_uri. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download peterpostmann/parse_uri
More information about peterpostmann/parse_uri
Files in peterpostmann/parse_uri
Package parse_uri
Short Description Parse a URI and return its components
License MIT
Informations about the package parse_uri
parse_uri
Parse a URI and return its components
This function parses a URI (RFC3986 URL, URN or Windows path) and returns an associative array containing any of the various components of the URL that are present. The values of the array elements are not URL decoded.
This function is not meant to validate the given URL, it only breaks it up into the above listed parts.
Install
Via Composer
If you dont want to use composer just copy the parse_uri.php
file and include it into your project.
Why
This function extends the capabailities of parse_url. It parses rfc complient URLs and URNs and (windows) file paths (basically everything which can be passed to file functions (e.g. fopen, file_get_contents)).
Usage
use function peterpostmann\uri\parse_uri;
array parse_uri ( string uri [, int $component = -2 [, bool $convertUrlToUrn = null ]])
Via option the output can be reduced to the output of parse_url
. Otherwise additonal components are provided:
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
will return
- {schema} if present
- 'file' if it is an absolute path ('/path')
- false if it is a relative path ('path/file', 'file')
- true if it is a Windows path ('C:\path')
- null if there is no path
Example
parse URIs
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:[email protected]: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:[email protected]:8888/path/to/file'));
var_dump(parse_uri('news:comp.infosystems.www.servers.unix'));
The above example will output:
patch URIs
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));
The above example will output:
Load References
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']
returns the URI without the fragment. ($components['_ressource']
without fragment and query).
$components['_protocol']
indicates the protocol. It usualy is the same as scheme, except for file:/// URIs. For absolute, relative and windows path, scheme is not set, but $components['_protocol']
is set to file. For URIs without schema \\example.org\path
the variable is set, but empty.
Helper Functions
build_uri
The function creates a uri (string) from its components.
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";
The above example will output:
convert_url2urn
The function converts php URLs which are not real URLs to URNs (what they should have been in first place).
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";
The above example will output:
If the URIs are parsed as-is, the parser will look for a host part which is not present. convert_url2urn
accepts a second parameter to control the conversion:
- null: Dont convert, except Formats from http://php.net/manual/en/wrappers.php (php://, compress.*://, zip://, zlib://, bzip2://, data://, glob://, phar://, rar://, ogg://, expect://)
- true: convert all
- false: convert nothing
License
The MIT License (MIT). Please see License File for more information.