PHPackages                             josantonius/url - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. josantonius/url

ActiveLibrary[HTTP &amp; Networking](/categories/http)

josantonius/url
===============

PHP library to access URL information.

v2.0.1(3y ago)123.2k↓33.3%52MITPHPPHP ^8.1

Since Feb 2Pushed 3y ago1 watchersCompare

[ Source](https://github.com/josantonius/php-url)[ Packagist](https://packagist.org/packages/josantonius/url)[ GitHub Sponsors](https://github.com/Josantonius)[ RSS](/packages/josantonius-url/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (17)Used By (2)

PHP URL library
===============

[](#php-url-library)

[![Latest Stable Version](https://camo.githubusercontent.com/ca5ecee2050630dd752adbf7d29703abe521da305129881ed2c0af421cce7b5b/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f75726c2f762f737461626c65)](https://packagist.org/packages/josantonius/url)[![License](https://camo.githubusercontent.com/25317fc07da2c56915903b4932727deb854bea084b3182b8b4707d386811183f/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f75726c2f6c6963656e7365)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/1a1e90565340b74257a4d82234646650e9a961ebcbd546224ff65d75a776a0d6/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f75726c2f646f776e6c6f616473)](https://packagist.org/packages/josantonius/url)[![CI](https://github.com/josantonius/php-url/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/josantonius/php-url/actions/workflows/ci.yml)[![CodeCov](https://camo.githubusercontent.com/22119f2af2d117c7dbff663267330a3c7aef0b5d215c49f0818b31ec7ac48081/68747470733a2f2f636f6465636f762e696f2f67682f6a6f73616e746f6e6975732f7068702d75726c2f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/josantonius/php-url)[![PSR1](https://camo.githubusercontent.com/b502a899c9aec217e98971160f816f87346be272cf1a25cfa4793f2ee724bfc8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d312d6635373034362e737667)](https://www.php-fig.org/psr/psr-1/)[![PSR4](https://camo.githubusercontent.com/d1c090de87e968254a6658528f3bfe9c9dad422d6047fd1323dc211560182c01/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d342d3962353962362e737667)](https://www.php-fig.org/psr/psr-4/)[![PSR12](https://camo.githubusercontent.com/19c529c6dc0656dcc2a16c1a84af450b7bd0dc7b0571b8f17e4fc9f2414f8821/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d31322d3161626339632e737667)](https://www.php-fig.org/psr/psr-12/)

**Translations**: [Español](.github/lang/es-ES/README.md)

PHP library to access URL information.

Provides an improved replacement for the access to the components of a URL offered by PHP's `parse_url` and `pathinfo` functions.

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`.

---

- [Requirements](#requirements)
- [Installation](#installation)
- [Available Classes](#available-classes)
    - [Url Class](#url-class)
- [Usage](#usage)
- [Tests](#tests)
- [TODO](#todo)
- [Changelog](#changelog)
- [Contribution](#contribution)
- [Sponsor](#sponsor)
- [License](#license)

---

Requirements
------------

[](#requirements)

- Operating System: Linux | Windows.
- PHP versions: 8.1 | 8.2.

Installation
------------

[](#installation)

The preferred way to install this extension is through [Composer](http://getcomposer.org/download/).

To install **PHP URL library**, simply:

```
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:

```
composer require josantonius/url --prefer-source
```

You can also **clone the complete repository** with Git:

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

Available Classes
-----------------

[](#available-classes)

### Url Class

[](#url-class)

`Josantonius\Url\Url`

Create a new instance:

```
/**
 * 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:

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

Gets the base URL:

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

Gets the path basename:

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

Gets the path dirname:

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

Gets the path basename extension:

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

Gets the path filename:

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

Gets fragment:

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

Gets the full URL:

```
public readonly string $full;
```

Gets hashed fragment:

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

Gets host:

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

Gets path:

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

Gets the query parameters:

```
/**
 * URL query parameters in array format.
 *
 * @var array URL query parameters or empty string.
 */
public readonly array $parameters;
```

Gets password:

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

Gets port:

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

Gets scheme:

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

Gets path segments:

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

Gets query:

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

Gets username:

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

Usage
-----

[](#usage)

Example of use for this library:

### Create a new instance using the current URL

[](#create-a-new-instance-using-the-current-url)

```
use Josantonius\Url\Url;

$url = new Url();
```

### Create a new instance using custom URL

[](#create-a-new-instance-using-custom-url)

```
use Josantonius\Url\Url;

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

### Gets authority

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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
-----

[](#tests)

To run [tests](tests) you just need [composer](http://getcomposer.org/download/)and to execute the following:

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

```
cd php-url
```

```
composer install
```

Run unit tests with [PHPUnit](https://phpunit.de/):

```
composer phpunit
```

Run code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):

```
composer phpcs
```

Run [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:

```
composer phpmd
```

Run all previous tests:

```
composer tests
```

TODO
----

[](#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
---------

[](#changelog)

Detailed changes for each release are documented in the [release notes](https://github.com/josantonius/php-url/releases).

Contribution
------------

[](#contribution)

Please make sure to read the [Contributing Guide](.github/CONTRIBUTING.md), before making a pull request, start a discussion or report a issue.

Thanks to all [contributors](https://github.com/josantonius/php-url/graphs/contributors)! ❤️

Sponsor
-------

[](#sponsor)

If this project helps you to reduce your development time, [you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work 😊

License
-------

[](#license)

This repository is licensed under the [MIT License](LICENSE).

Copyright © 2017-present, [Josantonius](https://github.com/josantonius#contact)

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 94.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~147 days

Recently: every ~431 days

Total

15

Last Release

1327d ago

Major Versions

1.2.1 → v2.0.02022-08-17

PHP version history (3 changes)1.0.0PHP &gt;=5.6

1.1.1PHP ^5.6 || ^7.0

v2.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b221283501ec8a9cbaefaf27821a91ae8ddd33bddf1fccc6c6815b7ad216ff1?d=identicon)[Josantonius](/maintainers/Josantonius)

---

Top Contributors

[![josantonius](https://avatars.githubusercontent.com/u/18104336?v=4)](https://github.com/josantonius "josantonius (53 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (3 commits)")

---

Tags

phpphp-urlurlurl-detailsurl-infohttpurluriphphttpspathparse

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/josantonius-url/health.svg)

```
[![Health](https://phpackages.com/badges/josantonius-url/health.svg)](https://phpackages.com/packages/josantonius-url)
```

###  Alternatives

[league/uri

URI manipulation library

1.1k206.4M277](/packages/league-uri)[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

536204.9M23](/packages/league-uri-interfaces)[crwlr/url

Swiss Army knife for URLs.

11073.3k3](/packages/crwlr-url)[workerman/psr7

PSR-7 message implementation that also provides common utility methods

1079.8k10](/packages/workerman-psr7)[elementaryframework/water-pipe

URL routing framework and requests/responses handler for PHP

254.6k4](/packages/elementaryframework-water-pipe)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
