2017 © Pedro Peláez
 

library php-uri

A PHP library for working with URIs (aka URLs), that is designed around the URI standard (RFC 3986). Requires PHP 5.4 or later. This library replaces and extends all of PHP's parse_url() features, and adds several new features for manipulating URI/URL strings.

image

projectcleverweb/php-uri

A PHP library for working with URIs (aka URLs), that is designed around the URI standard (RFC 3986). Requires PHP 5.4 or later. This library replaces and extends all of PHP's parse_url() features, and adds several new features for manipulating URI/URL strings.

  • Sunday, October 5, 2014
  • by ProjectCleverWeb
  • Repository
  • 2 Watchers
  • 3 Stars
  • 237 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 3 % Grown

The README.md

PHP URI Build Status Code Coverage Scrutinizer Code Quality License

A PHP library for working with URI's, that is designed around the URI standard. Requires PHP 5.4 or later. This library replaces and extends all of PHP's parse_url() features, and even has some handy aliases., (*1)

Copyright (c) 2014 Nicholas Jordon - All Rights Reserved., (*2)

Installing The Library

Composer:

Add this to your composer.json file, (*3)

"require": {
    "projectcleverweb/php-uri":"~1.0"
}

Manual:

Download:
Latest Stable Version Latest Unstable Version, (*4)

Just include the uri.lib.php file somewhere in your application., (*5)

Examples

Example #1: String Operations

$uri = new uri('http://example.com/path/to/file.ext');

$uri->replace('QUERY', 'number=3');
$uri->replace('PATH', '/foo/bar');
$uri->append('PATH', '.baz');
$new = $uri->prepend('HOST', 'www.');

$uri->reset();
$original = $uri->str();

$uri->replace('FRAGMENT', 'Checkout');
$secure = $uri->replace('SCHEME_NAME', 'https');

echo $new.PHP_EOL;
echo $original.PHP_EOL;
echo $secure;

Output:, (*6)

http://www.example.com/foo/bar.baz?number=3
http://example.com/path/to/file.ext
https://example.com/path/to/file.ext#Checkout

Example #2: Daisy Chaining Operations

Need to change a lot while keeping anything extra intact? Chain it., (*7)

$uri1 = new uri('ftp://jdoe:pass1234@my-server.com/public_html');

// Lets upgrade to an admin account under sftp, but stay in the current directory.
$uri1->chain()->
    prepend('SCHEME_NAME', 's')->
    replace('PORT', '22')->
    replace('USER', 'admin')->
    replace('PASS', 'secure-pass-123');

// NOTE: chain() methods always return the chain object, even if a method fails.
echo $uri1;

// Any failure results in the chain() error count geting incremented.
if (0 < $uri->chain()->error_count) {
    print_f('The chain failed %1$s times!', $uri->chain()->error_count);
}

Output:, (*8)

sftp://admin:secure-pass-123@my-server.com:22/public_html

Example #3: Information Gathering

$uri = new uri('http://example.com/path/to/file.ext?q=1');

if ($uri->scheme_name == 'https') {
    echo 'Uses SSL'.PHP_EOL;
} else {
    echo 'Does not use SSL'.PHP_EOL;
}

// Change to an absolute path
$abs_path = $_SERVER['DOCUMENT_ROOT'].$uri->path;
echo $abs_path.PHP_EOL;

// easier to read links
printf('<a href="%1$s">%2$s</a>', $uri->str(), $uri->host.$uri->path);

// FTP logins
$uri = new uri('ftp://jdoe@example.com/my/home/dir');
$login = array(
    'username' => $uri->user,
    'password' => $user_input,
    'domain'   => $uri->host,
    'path'     => $uri->path
);

Output:, (*9)

Does not use SSL
/var/www/path/to/file.ext
<a href="http://example.com/path/to/file.ext?q=1">example.com/path/to/file.ext</a>

Example #4: Works With A Wide Range Of URIs

Works perfectly with email, skype, and ssh URIs. The parser is based directly off the URI standard, so it will work well with uncommon and new URI types., (*10)

$uri1 = new uri('git@github.com:ProjectCleverWeb/PHP-URI.git');
$uri2 = new uri('example@gmail.com');

// Publish you source to multiple services?
echo $uri1.PHP_EOL; // PHP will automatically get the current URI
echo $uri1->replace('HOST', 'gitlab.com').PHP_EOL;
echo $uri1->replace('HOST', 'bitbucket.org').PHP_EOL.PHP_EOL;

// Quick and easy email template URI
$uri2->chain()
    ->replace('SCHEME', 'mailto:')
    ->query_replace('subject', 'Re: [Suggestion Box]')
    ->query_replace('body', 'More snickers in the break room please!')
;
printf('<a href="%1$s">%2$s</a>', $uri2, $uri2->authority);

Output:, (*11)

git@github.com:ProjectCleverWeb/PHP-URI.git
git@gitlab.com:ProjectCleverWeb/PHP-URI.git
git@bitbucket.org:ProjectCleverWeb/PHP-URI.git

<a href="mailto:example@gmail.com?subject=Re%3A%20%5BSuggestion%20Box%5D&body=More%20snickers%20in%20the%20break%20room%20please%21">example@gmail.com</a>

Known Issues

  • Cloning doesn't work as expected (use $clone = new \uri($original->str()); $clone->input = $original->input; instead)
  • You cannot directly change the authority of a URI. This is intentional, as the authority is generated from the current URI.

License

The MIT License (MIT), (*12)

Copyright (c) 2014 Nicholas Jordon - All Rights Reserved, (*13)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*14)

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*15)

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*16)

The Versions

05/10 2014

dev-feature/data_object

dev-feature/data_object https://github.com/ProjectCleverWeb/PHP-URI

A PHP library for working with URIs (aka URLs), that is designed around the URI standard (RFC 3986). Requires PHP 5.4 or later. This library replaces and extends all of PHP's parse_url() features, and adds several new features for manipulating URI/URL strings.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

url address web uri parse_url

26/09 2014

dev-develop

dev-develop https://github.com/ProjectCleverWeb/PHP-URI

A PHP library for working with URIs (aka URLs), that is designed around the URI standard (RFC 3986). Requires PHP 5.4 or later. This library replaces and extends all of PHP's parse_url() features, and adds several new features for manipulating URI/URL strings.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

url address web uri parse_url

06/08 2014

dev-master

9999999-dev https://github.com/ProjectCleverWeb/PHP-URI

A PHP library for working with URI's, that is designed around the URI standard. Requires PHP 5.4 or later. This library replaces and extends all of PHP's parse_url() features, and even has some handy aliases.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

url address web uri parse_url

06/08 2014

dev-release/1.0.0

dev-release/1.0.0 https://github.com/ProjectCleverWeb/PHP-URI

A PHP library for working with URI's, that is designed around the URI standard. Requires PHP 5.4 or later. This library replaces and extends all of PHP's parse_url() features, and even has some handy aliases.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

url address web uri parse_url

06/08 2014

1.0.0

1.0.0.0 https://github.com/ProjectCleverWeb/PHP-URI

A PHP library for working with URI's, that is designed around the URI standard. Requires PHP 5.4 or later. This library replaces and extends all of PHP's parse_url() features, and even has some handy aliases.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

url address web uri parse_url

28/07 2014

1.0.0-RC2

1.0.0.0-RC2 https://github.com/ProjectCleverWeb/PHP-URI

A PHP library for working with URI's. Requires PHP 5.3.7 or later. Replaces and extends PHP's parse_url().

  Sources   Download

MIT

The Requires

  • php >=5.3.7

 

The Development Requires

url address web uri parse_url