The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Uri parser

Uri schemes

Uri manipulations

Uri components

Upgrading Guide

Public API

URI parsing

Uri\parse is available since version 1.1.0

URI parsing can be done:

Learn more about the differences with PHP’s parse_url in the parser section.

<?php

use League\Uri;

$parser = new Uri\Parser();
var_export($parser('http://foo.com?@bar.com/'));
//returns the following array
//array(
//  'scheme' => 'http',
//  'user' => null,
//  'pass' => null,
//  'host' => 'foo.com',
//  'port' => null,
//  'path' => '',
//  'query' => '@bar.com/',
//  'fragment' => null,
//);

var_export(Uri\parse('http://foo.com?@bar.com/'));
//returns the following array
//array(
//  'scheme' => 'http',
//  'user' => null,
//  'pass' => null,
//  'host' => 'foo.com',
//  'port' => null,
//  'path' => '',
//  'query' => '@bar.com/',
//  'fragment' => null,
//);

URI building

Uri\build is available since version 1.1.0

You can rebuild a URI from a array using the helper function Uri\build.

<?php

use League\Uri;

$uri = Uri\build([
    'scheme' => 'http',
    'user' => null,
    'pass' => null,
    'host' => 'foo.com',
    'port' => null,
    'path' => '',
    'query' => '@bar.com/',
    'fragment' => null,
]);

echo $uri; //displays http://foo.com?@bar.com/

The Uri\build function never output the pass component as suggested by RFC3986.

Scheme validation

available since version 1.2.0

You can validate any scheme component using:

<?php

use League\Uri;

$parser = new Uri\Parser();
$parser->isScheme('example.com'); //returns false
$parser->isScheme('ssh+svn'); //returns true

Uri\is_scheme('data');  //returns true
Uri\is_scheme('data:'); //returns false

Host validation

Uri\is_host is available since version 1.1.0

You can validate any host component using:

Learn more about host validation in the parser section.

<?php

use League\Uri;

$parser = new Uri\Parser();
$parser->isHost('example.com'); //returns true
$parser->isHost('/path/to/yes'); //returns false

Uri\is_host('[:]'); //returns true
Uri\is_host('[127.0.0.1]'); //returns false

Port validation

available since version 1.2.0

You can validate any port component using:

<?php

use League\Uri;

$parser = new Uri\Parser();
$parser->isPort('example.com'); //returns false
$parser->isPort(888);           //returns true

Uri\is_port('23');    //returns true
Uri\is_port('data:'); //returns false