PHPackages                             peterpostmann/parse\_uri - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. peterpostmann/parse\_uri

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

peterpostmann/parse\_uri
========================

Parse a URI and return its components

1.0.0(8y ago)1225.1k↑74.1%4MITPHP

Since Nov 17Pushed 8y ago1 watchersCompare

[ Source](https://github.com/peterpostmann/php-parse_uri)[ Packagist](https://packagist.org/packages/peterpostmann/parse_uri)[ RSS](/packages/peterpostmann-parse-uri/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (4)

parse\_uri
==========

[](#parse_uri)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/b8503c15758f202b9b7b8fb8b84d1bdf0f2238e403263acd3f0d15645642adc3/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7065746572706f73746d616e6e2f7068702d70617273655f7572692f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/peterpostmann/php-parse_uri)

Parse a URI and return its components

This function parses a URI ([RFC3986](https://tools.ietf.org/html/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
-------

[](#install)

### Via Composer

[](#via-composer)

```
composer require peterpostmann/parse_uri
```

If you dont want to use composer just copy the `parse_uri.php` file and include it into your project.

Why
---

[](#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
-----

[](#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

[](#example)

#### parse URIs

[](#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: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'));
```

The above example will output:

```
# 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 URIs

[](#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:

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

#### Load References

[](#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

[](#helper-functions)

#### build\_uri

[](#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:

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

#### convert\_url2urn

[](#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:

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

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  (php://, compress.\*://, zip://, zlib://, bzip2://, data://, glob://, phar://, rar://, ogg://, expect://)
- true: convert all
- false: convert nothing

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3104d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9362588b9edca85b56850256b890ab5e0f94670fa50e460a9547192785eb880b?d=identicon)[peterpostmann](/maintainers/peterpostmann)

---

Top Contributors

[![peterpostmann](https://avatars.githubusercontent.com/u/9541377?v=4)](https://github.com/peterpostmann "peterpostmann (5 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/peterpostmann-parse-uri/health.svg)

```
[![Health](https://phpackages.com/badges/peterpostmann-parse-uri/health.svg)](https://phpackages.com/packages/peterpostmann-parse-uri)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[meyfa/php-svg

Read, edit, write, and render SVG files with PHP

54613.9M42](/packages/meyfa-php-svg)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
