PHPackages                             wpscholar/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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. wpscholar/url

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

wpscholar/url
=============

A PHP class for parsing and manipulating URLs.

1.2.5(1y ago)4219.8k—5.1%1[1 PRs](https://github.com/wpscholar/url/pulls)GPL-2.0+PHPPHP &gt;=7.2CI passing

Since Mar 31Pushed 5mo ago1 watchersCompare

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

READMEChangelog (7)Dependencies (1)Versions (12)Used By (0)

URL Handler
===========

[](#url-handler)

[![Tests](https://github.com/wpscholar/url/actions/workflows/tests.yml/badge.svg)](https://github.com/wpscholar/url/actions/workflows/tests.yml)[![Latest Stable Version](https://camo.githubusercontent.com/ee0bb6875fc5bb41c19df3950b291000ac41209781c630d4e5947956e486c10a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f77707363686f6c61722f75726c2e737667)](https://packagist.org/packages/wpscholar/url)[![PHP Version Support](https://camo.githubusercontent.com/7fe5e9f79fc1d389e5ee6889399d57b96cbfdfb4dc0ea57aa6868ba37060e577/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344372e322d626c7565)](https://packagist.org/packages/wpscholar/url)[![License](https://camo.githubusercontent.com/c01db53e1da434fe0702a37c7529adeb5eb6b2fa15a1370a03b5bda15757b36e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f77707363686f6c61722f75726c2e737667)](https://packagist.org/packages/wpscholar/url)[![codecov](https://camo.githubusercontent.com/811415c3a1bb95bcd0ad12188c119c47836f00f228031b95cb31c456654f20c3/68747470733a2f2f636f6465636f762e696f2f67682f77707363686f6c61722f75726c2f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/wpscholar/url)

A PHP library for parsing, manipulating, and building URLs.

Description
-----------

[](#description)

This library provides a simple and intuitive way to work with URLs in PHP. It allows you to parse, manipulate, and build URLs while handling all the common URL components including scheme, host, port, path, query parameters, and fragments.

Features
--------

[](#features)

- Parse URLs into their component parts
- Build URLs from component parts
- Add, remove, and modify query parameters
- Handle URL fragments
- Detect and manipulate trailing slashes
- Get current URL and scheme detection
- Path segment manipulation
- URL string conversion

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

[](#requirements)

- PHP 7.2 or higher

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

[](#installation)

Install via Composer:

```
composer require wpscholar/url
```

Testing
-------

[](#testing)

This library includes a comprehensive test suite built with PHPUnit. To run the tests:

1. Install development dependencies:

```
composer install
```

2. Run the test suite:

```
composer test
```

The test suite covers all major functionality including:

- URL parsing
- Query parameter manipulation
- Static helper methods
- Path manipulation
- URL output methods

The test suite is automatically run via GitHub Actions whenever code is pushed to the `main` branch or when a pull request is created. The tests are run against multiple PHP versions (7.2, 7.3, 7.4, 8.0, 8.1, 8.2, and 8.3) to ensure broad compatibility.

Basic Usage
-----------

[](#basic-usage)

```
use wpscholar\Url;

// Create from a URL string
$url = new Url('https://example.com/path?param=value#section');

// Get the current URL
$currentUrl = new Url(); // Automatically uses current URL

// Access URL components
echo $url->scheme; // 'https'
echo $url->host; // 'example.com'
echo $url->path; // '/path'
echo $url->query; // 'param=value'
echo $url->fragment; // 'section'

// Modify query parameters
$url->addQueryVar('new_param', 'value');
$url->removeQueryVar('old_param');

// Get specific query parameter
$value = $url->getQueryVar('param_name');

// Get all query parameters as array
$params = $url->getQueryVars();
```

Static Helpers
--------------

[](#static-helpers)

```
// Get current URL
$currentUrl = Url::getCurrentUrl();

// Get current scheme (http/https)
$scheme = Url::getCurrentScheme();

// Strip query string from URL
$cleanUrl = Url::stripQueryString($url);

// Build URL from parts
$url = Url::buildUrl([
    'scheme' => 'https',
    'host' => 'example.com',
    'path' => '/path',
    'query' => 'param=value'
]);
```

Path Manipulation
-----------------

[](#path-manipulation)

```
// Given URL: https://example.com/blog/2023/post-title

// Get all path segments as array
$segments = $url->getSegments();
// Returns: ['blog', '2023', 'post-title']

// Get specific segment by index (zero-based)
$year = $url->getSegment(1);     // Returns: '2023'
$section = $url->getSegment(0);  // Returns: 'blog'
$slug = $url->getSegment(2);     // Returns: 'post-title'
```

URL Output
----------

[](#url-output)

```
// Get full URL as string - multiple methods:
$url = new Url('https://example.com/path?param=value');

// Method 1: Using toString()
echo $url->toString();  // 'https://example.com/path?param=value'

// Method 2: Cast to string directly
echo (string) $url;     // 'https://example.com/path?param=value'

// Method 3: Using magic __toString()
echo $url;              // 'https://example.com/path?param=value'

// Get URL parts as array
$urlParts = $url->toArray();  // Returns array of URL components
```

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance56

Moderate activity, may be stable

Popularity39

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 93.8% 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 ~353 days

Recently: every ~377 days

Total

10

Last Release

521d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ae89dc177002c2f67341cde2d525e3f3c5b1d6aaaf07df15257a284f001c5551?d=identicon)[wpscholar](/maintainers/wpscholar)

---

Top Contributors

[![wpscholar](https://avatars.githubusercontent.com/u/890951?v=4)](https://github.com/wpscholar "wpscholar (30 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

composer-packageurl-parserurl-parsing

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[meyfa/php-svg

Read, edit, write, and render SVG files with PHP

54613.9M42](/packages/meyfa-php-svg)

PHPackages © 2026

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