--- layout: default title: RFC3986 - RFC3987 Parser redirect_from: - /5.0/parser/ - /parser/ --- URI String parser and builder ======= [![Build Status](https://img.shields.io/travis/thephpleague/uri-parser/master.svg?style=flat-square)](https://travis-ci.org/thephpleague/uri-parser) [![Latest Version](https://img.shields.io/github/release/thephpleague/uri-parser.svg?style=flat-square)](https://github.com/thephpleague/uri-parser/releases) This package contains - a userland PHP uri parser compliant with [RFC 3986](http://tools.ietf.org/html/rfc3986) and [RFC 3987](http://tools.ietf.org/html/rfc3987) to replace PHP's `parse_url` function. - helper functions to ease parsing and building URI. ## System Requirements You need: - **PHP >= 7.0** but the latest stable version of PHP is recommended While the library no longer requires out of the box the `intl` extension starting with version `1.4.0` to work, you still require it if you are dealing with URIs containing non-ASCII host. Without it, the parser will throw an exception if such URI is parsed. ## Installation ~~~bash $ composer require league/uri-parser ~~~ ## URI parsing

available since version 1.3.0 Parser::parse is an alias of Parser::__invoke.

The `Parser::__invoke` method is a drop-in replacement to PHP's `parse_url` function, with the following differences: ### The parser is RFC3986/RFC3987 compliant ~~~php 'http', // 'user' => null, // 'pass' => null, // 'host' => 'foo.com', // 'port' => null, // 'path' => '', // 'query' => '@bar.com/', // 'fragment' => null, //); var_export(parse_url('http://foo.com?@bar.com/')); //returns the following array //array( // 'scheme' => 'http', // 'host' => 'bar.com', // 'user' => 'foo.com?', // 'path' => '/', //); // Depending on the PHP version ~~~ ### The Parser returns all URI components. ~~~php 'http', // 'user' => null, // 'pass' => null, // 'host' => 'www.example.com', // 'port' => null, // 'path' => '/', // 'query' => null, // 'fragment' => null, //); var_export(parse_url('http://www.example.com/')); //returns the following array //array( // 'scheme' => 'http', // 'host' => 'www.example.com', // 'path' => '/', //); ~~~ ### No extra parameters needed ~~~php You still need to validate them against its scheme specific rules.

~~~php 'http', // 'user' => null, // 'pass' => null, // 'host' => null, // 'port' => null, // 'path' => 'www.example.com', // 'query' => null, // 'fragment' => null, //); ~~~

This invalid HTTP URI is successfully parsed.

### function alias

available since version 1.1.0

The library also provides a function alias to `Parser::__invoke`, `Uri\parse`: ~~~php 'http', // 'user' => null, // 'pass' => null, // 'host' => 'foo.com', // 'port' => null, // 'path' => '', // 'query' => '@bar.com/', // 'fragment' => null, //); ~~~ ## URI Building ~~~php Uri\build is available since version 1.1.0

You can rebuild a URI from its hash representation returned by the `Parser::__invoke` method or PHP's `parse_url` function using the helper function `Uri\build`. If you supply your own hash you are responsible for providing valid encoded components without their URI delimiters. ~~~php 'http', // 'user' => 'hello', // 'pass' => 'world', // 'host' => 'foo.com', // 'port' => null, // 'path' => '', // 'query' => '@bar.com/', // 'fragment' => null, //); $uri = build($components); echo $uri; //displays http://hello@foo.com?@bar.com/ ~~~ The `Uri\build` function never output the `pass` component as suggested by [RFC3986](https://tools.ietf.org/html/rfc3986#section-7.5). ## Scheme validation

available since version 1.2.0

If you have a scheme **string** you can validate it against the parser. The scheme is considered to be valid if it is: - an empty string; - a string which follow [RFC3986 rules](https://tools.ietf.org/html/rfc3986#section-3.1); ~~~php isScheme('example.com'); //returns false $parser->isScheme('ssh+svn'); //returns true $parser->isScheme('data'); //returns true $parser->isScheme('data:'); //returns false ~~~ The library also provides a function alias `Uri\is_scheme`: ~~~php isHost('example.com'); //returns true $parser->isHost('/path/to/yes'); //returns false $parser->isHost('[:]'); //returns true $parser->isHost('[127.0.0.1]'); //returns false ~~~ The library also provides a function alias `Uri\is_host`: ~~~php available since version 1.2.0

If you have a port, you can validate it against the parser. The port is considered to be valid if it is: - a numeric value which follow [RFC3986 rules](https://tools.ietf.org/html/rfc3986#section-3.2.3); ~~~php isPort('example.com'); //returns false $parser->isPort(888); //returns true $parser->isPort('23'); //returns true $parser->isPort('data:'); //returns false ~~~ The library also provides a function alias `Uri\is_port`: ~~~php