2017 © Pedro Peláez
 

library url

Library for urls manipulation.

image

josantonius/url

Library for urls manipulation.

  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 14 Versions
  • 2 % Grown

The README.md

PHP URL library

Latest Stable Version License Total Downloads CI CodeCov PSR1 PSR4 PSR12, (*1)

Translations: Español, (*2)

PHP library to access URL information., (*3)

Provides an improved replacement for the access to the components of a URL offered by PHP's parse_url and pathinfo functions., (*4)

This library does not format the provided URL, it only makes it easier to access the components. For something more advanced you can use something like league/uri-components., (*5)



Requirements

  • Operating System: Linux | Windows., (*6)

  • PHP versions: 8.1 | 8.2., (*7)

Installation

The preferred way to install this extension is through Composer., (*8)

To install PHP URL library, simply:, (*9)

composer require josantonius/url

The previous command will only install the necessary files, if you prefer to download the entire source code you can use:, (*10)

composer require josantonius/url --prefer-source

You can also clone the complete repository with Git:, (*11)

git clone https://github.com/josantonius/php-url.git

Available Classes

Url Class

Josantonius\Url\Url, (*12)

Create a new instance:, (*13)

/**
 * If no URL is provided, the URL of the current page will be generated.
 * 
 * The generated URL will exclude ports 80 and 443 and include the rest.
 */
public function __construct(null|string $url = null);

Gets authority:, (*14)

/**
 * The authority, in "[user-info@][host][:port]" format.
 *
 * @var string URL authority or empty string.
 */
public readonly string $authority;

Gets the base URL:, (*15)

/**
 * The base URL, in "[scheme:][//domain][:port]" format.
 *
 * @var string Base URL or empty string.
 */
public readonly string $base;

Gets the path basename:, (*16)

/**
 * The path basename, in "[filename][.extension]" format.
 *
 * @var string URL path basename or empty string.
 */
public readonly string $basename;

Gets the path dirname:, (*17)

/**
 * The path dirname, in "[dirname]" format.
 *
 * @var string URL path dirname or empty string.
 */
public readonly string $dirname;

Gets the path basename extension:, (*18)

/**
 * The path basename extension, in "[extension]" format.
 *
 * @var string URL path basename extension or empty string.
 */
public readonly string $extension;

Gets the path filename:, (*19)

/**
 * The path filename, in "[filename]" format.
 *
 * @var string URL path filename or empty string.
 */
public readonly string $filename;

Gets fragment:, (*20)

/**
 * URL fragment in "[fragment]" format.
 *
 * @var string URL fragment or empty string.
 */
public readonly string $fragment;

Gets the full URL:, (*21)

public readonly string $full;

Gets hashed fragment:, (*22)

/**
 * URL hashed fragment in "[#fragment]" format.
 *
 * @var string URL hashed fragment or empty string.
 */
public readonly string $hash;

Gets host:, (*23)

/**
 * URL host in "[subdomain.][domain][.tld]" format.
 *
 * @var string URL host or empty string.
 */
public readonly string $host;

Gets path:, (*24)

/**
 * URL path in "[path]" format.
 *
 * @var string URL path or empty string.
 */
public readonly string $path;

Gets the query parameters:, (*25)

/**
 * URL query parameters in array format.
 *
 * @var array<string, mixed> URL query parameters or empty string.
 */
public readonly array $parameters;

Gets password:, (*26)

/**
 * URL password in "[password]" format.
 *
 * @var string URL password or empty string.
 */
public readonly string $password;

Gets port:, (*27)

/**
 * URL port in "[port]" format.
 *
 * @var string URL port or empty string.
 */
public readonly int|string $port;

Gets scheme:, (*28)

/**
 * URL scheme in "[scheme]" format.
 *
 * @var string URL scheme or empty string.
 */
public readonly string $scheme;

Gets path segments:, (*29)

/**
 * URL path segments in array format.
 *
 * @var string[] URL path segments or empty string.
 */
public readonly array $segments;

Gets query:, (*30)

/**
 * URL query in "[query]" format.
 *
 * @var string URL query or empty string.
 */
public readonly string $query;

Gets username:, (*31)

/**
 * URL username in "[username]" format.
 *
 * @var string URL username or empty string.
 */
public readonly string $username;

Usage

Example of use for this library:, (*32)

Create a new instance using the current URL

use Josantonius\Url\Url;

$url = new Url();

Create a new instance using custom URL

use Josantonius\Url\Url;

$url = new Url('https://domain.com');

Gets authority

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com

$url->authority;  // "domain.com"


$url = new Url('https://user:pass@sub.domain.com:90/en/');

$url->authority; // "user:pass@sub.domain.com:90"


$url = new Url('https://user:pass@sub.domain.com/en/');

$url->authority; // "user:pass@sub.domain.com"


$url = new Url('https://sub.domain.com/en/');

$url->authority; // "sub.domain.com"

Gets base URL

use Josantonius\Url\Url;

$url = new Url(); // https://user:pass@domain.com:80/en/

$url->base; // "https://domain.com"


$url = new Url('https://domain.com:80/?tag=bug');

$url->base; // "https://domain.com:80"


$url = new Url('https://domain.com/en/');

$url->base; // "https://domain.com"

Gets the path basename

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/search.php

$url->basename; // "search.php"


$url = new Url('https://domain.com/en/web/docs/search.php?tag=bug');

$url->basename; // "search.php"


$url = new Url('https://domain.com/en/web/docs?tag=bug');

$url->basename; // "docs"

Gets the path dirname

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/search.php

$url->dirname; // "/"


$url = new Url('https://domain.com/en/web/docs/search.php?tag=bug');

$url->dirname; // "/en/web/docs"


$url = new Url('https://domain.com/en/web/docs?tag=bug');

$url->dirname; // "/en/web"

Gets the path basename extension

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/search.php

$url->extension; // "php"


$url = new Url('https://domain.com/en/web/docs/search.php?tag=bug');

$url->extension; // "php"


$url = new Url('https://domain.com/en/web/docs?tag=bug');

$url->extension; // ""

Gets the path filename

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/search.php

$url->filename; // "search"


$url = new Url('https://domain.com/en/web/docs/search.php?tag=bug');

$url->filename; // "search"


$url = new Url('https://domain.com/docs?tag=bug');

$url->filename; // "docs"

Gets fragment

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com#top

$url->fragment; // "top"


$url = new Url('https://domain.com/en/web/docs#top');

$url->fragment; // "top"


$url = new Url('https://domain.com');

$url->fragment; // ""

Gets the full URL

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com:80

$url->full;  // "https://domain.com"


$url = new Url('https://user:pass@sub.domain.com:90/en/');

$url->full; // "https://user:pass@sub.domain.com:90/en/"

Gets hashed fragment

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com#top

$url->hash; // "#top"


$url = new Url('https://domain.com/en/web/docs#top');

$url->hash; // "#top"


$url = new Url('https://domain.com');

$url->hash; // ""

Gets host

use Josantonius\Url\Url;

$url = new Url(); // https://sub.domain.com

$url->host; // "sub.domain.com"


$url = new Url('https://sub.domain.com/en/web/docs#top');

$url->host; // "sub.domain.com"


$url = new Url('https://domain.com');

$url->host; // "domain.com"


$url = new Url('https://localhost');

$url->host; // "localhost"

Gets path

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/en

$url->path; // "/en/web/docs/search.php"


$url = new Url('https://domain.com/en/web/docs/search.php');

$url->path; // "/en/web/docs/search.php"


$url = new Url('https://domain.com/en/web/docs/');

$url->path; // "/en/web/docs/"


$url = new Url('https://domain.com/en?tag=bug');

$url->path; // "/en"


$url = new Url('https://domain.com?tag=bug');

$url->path; // ""

Gets the query parameters

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/en?tag=bug&order=asc#top

$url->parameters; // ["tag" => "bug", "order" => "asc"]


$url = new Url('https://domain.com/en/web/docs/search.php');

$url->parameters; // ""

Gets password

use Josantonius\Url\Url;

$url = new Url(); // https://:pass@domain.com

$url->password; // "pass"


$url = new Url('https://user:pass@domain.com');

$url->password; // "pass"


$url = new Url('https://user@domain.com');

$url->password; // ""

Gets port

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com:90

$url->port; // 90


$url = new Url(); // https://domain.com:80

$url->port; // ""


$url = new Url(); // https://domain.com:443

$url->port; // ""


$url = new Url('https://domain.com:80/en/');

$url->port; // 80


$url = new Url('https://domain.com:443/en/');

$url->port; // 443


$url = new Url('https://domain.com/en/');

$url->port; // ""

Gets scheme

use Josantonius\Url\Url;

$url = new Url(); // http://domain.com

$url->scheme; // "http"


$url = new Url('https://domain.com');

$url->scheme; // "https"

Gets path segments

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com?tag=bug

$url->segments; // []


$url = new Url('https://domain.com/en/web/docs/search.php');

$url->segments; // ['en', 'web', 'docs', 'search.php']

Gets query

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com?tag=bug

$url->query; // "tag=bug"


$url = new Url('https://domain.com?tag=bug&order=asc#top');

$url->query; // "tag=bug&order=asc"

$url = new Url('https://domain.com');

$url->query; // ""

Gets username

use Josantonius\Url\Url;

$url = new Url(); // https://user@domain.com

$url->username; // "user"


$url = new Url('https://:pass@domain.com');

$url->username; // ""


$url = new Url('https://user:pass@domain.com');

$url->username; // "user"


$url = new Url('https://domain.com');

$url->username; // ""

Tests

To run tests you just need composer and to execute the following:, (*33)

git clone https://github.com/josantonius/php-url.git
cd php-url
composer install

Run unit tests with PHPUnit:, (*34)

composer phpunit

Run code standard tests with PHPCS:, (*35)

composer phpcs

Run PHP Mess Detector tests to detect inconsistencies in code style:, (*36)

composer phpmd

Run all previous tests:, (*37)

composer tests

TODO

  • [ ] Add new feature
  • [ ] Improve tests
  • [ ] Improve documentation
  • [ ] Improve English translation in the README file
  • [ ] Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)

Changelog

Detailed changes for each release are documented in the release notes., (*38)

Contribution

Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue., (*39)

Thanks to all contributors! :heart:, (*40)

If this project helps you to reduce your development time, you can sponsor me to support my open source work :blush:, (*41)

License

This repository is licensed under the MIT License., (*42)

Copyright © 2017-present, Josantonius, (*43)

The Versions

17/04 2018

dev-master

9999999-dev

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

url https php hhvm uri path

17/04 2018

1.2.1

1.2.1.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

url https php hhvm uri path

22/02 2018

1.2.0

1.2.0.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

url https php hhvm uri path

07/01 2018

1.1.9

1.1.9.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

url https php hhvm uri path

12/11 2017

1.1.8

1.1.8.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

url https php hhvm uri path

09/11 2017

1.1.7

1.1.7.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

url https php hhvm uri path

03/11 2017

1.1.6

1.1.6.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

url https php hhvm uri path

18/09 2017

1.1.5

1.1.5.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

url https php hhvm uri path

18/07 2017

1.1.4

1.1.4.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

url https php hhvm uri path

09/07 2017

1.1.3

1.1.3.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

url https php hhvm uri path

09/05 2017

1.1.2

1.1.2.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

url https php hhvm uri path

18/03 2017

1.1.1

1.1.1.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

url https php hhvm uri path

28/02 2017

1.1.0

1.1.0.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

url https php hhvm uri path

02/02 2017

1.0.0

1.0.0.0

Library for urls manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

url https php hhvm uri path