This package contains a userland PHP URI string parser and builder compliant with:
<?php
use League\Uri\UriString;
var_export(UriString::parse('https://www.example.com/'));
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'www.example.com',
// 'port' => null,
// 'path' => '/',
// 'query' => null,
// 'fragment' => null,
//);
You need PHP >= 7.1.3 but the latest stable version of PHP is recommended.
While the library no longer requires the ext/intl
extension, it is strongly recommended to install this extension if you are dealing with URIs containing non-ASCII host. Without the extension, the parser will throw a InvalidURI
exception when trying to parse such URI.
$ composer require league/uri-parser
<?php
public static function League\Uri\UriString::parse($uri): array
The UriString::parse
static method is a drop-in replacement to PHP's parse_url
function, with the following differences:
<?php
use League\Uri\UriString;
var_export(UriString::parse('https://[email protected]/'));
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'foo.com',
// 'port' => null,
// 'path' => '',
// 'query' => '@bar.com/',
// 'fragment' => null,
//);
var_export(parse_url('https://[email protected]/'));
//returns the following array
//array(
// 'scheme' => 'http',
// 'host' => 'bar.com',
// 'user' => 'foo.com?',
// 'path' => '/',
//);
// Depending on the PHP version
<?php
use League\Uri\UriString;
var_export(UriString::parse('https://www.example.com/'));
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'www.example.com',
// 'port' => null,
// 'path' => '/',
// 'query' => null,
// 'fragment' => null,
//);
var_export(parse_url('https://www.example.com/'));
//returns the following array
//array(
// 'scheme' => 'http',
// 'host' => 'www.example.com',
// 'path' => '/',
//);
<?php
use League\Uri\UriString;
$uri = 'https://www.example.com/';
UriString::parse($uri)['query']; //returns null
parse_url($uri, PHP_URL_QUERY); //returns null
A distinction is made between an unspecified component, which will be set to null
and an empty component which will be equal to the empty string.
<?php
use League\Uri\UriString;
$uri = 'https://www.example.com?';
UriString::parse($uri)['query']; //returns ''
parse_url($uri, PHP_URL_QUERY); //returns null
Since a URI is made of at least a path component, this component is never equal to null
<?php
use League\Uri\UriString;
$uri = 'https://www.example.com?';
UriString::parse($uri)['path']; //returns ''
parse_url($uri, PHP_URL_PATH); //returns null
<?php
use League\Uri\UriString;
$uri = '//example.com:toto';
UriString::parse($uri);
//throw a League\Uri\Exception\InvalidURI
parse_url($uri); //returns false
Just like parse_url
, UriString::parse
only parses and extracts components from the URI string.
You still need to validate them against its scheme specific rules.
<?php
use League\Uri\UriString;
$uri = 'http:www.example.com';
var_export(UriString::parse($uri));
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => null,
// 'port' => null,
// 'path' => 'www.example.com',
// 'query' => null,
// 'fragment' => null,
//);
<?php
public static function League\Uri\UriString::build(array $components): string
You can rebuild a URI from its hash representation returned by the UriString::parse
method or PHP's parse_url
function using the UriString::build
public static method.
If you supply your own hash you are responsible for providing valid encoded components without their URI delimiters.
<?php
use League\Uri\UriString;
$base_uri = 'https://hello:[email protected][email protected]/';
$components = UriString::parse($base_uri);
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => 'hello',
// 'pass' => 'world',
// 'host' => 'foo.com',
// 'port' => null,
// 'path' => '',
// 'query' => '@bar.com/',
// 'fragment' => null,
//);
$uri = UriString::build($components);
echo $uri; //displays https://[email protected][email protected]/
The library has a :
- a PHPUnit test suite
- a coding style compliance test suite using PHP CS Fixer.
- a code analysis compliance test suite using PHPStan.
To run the tests, run the following command from the project folder.
$ composer test
Contributions are welcome and will be fully credited. Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.