PHP's URI extension can give you a URL's path as a single string, but if you want each part of the path, you have to split it yourself. This RFC from Máté Kocsis adds methods that return the path as an array of segments, where a segment is one piece between the slashes. Uri\Rfc3986\Uri already stores the path as segments internally, so splitting it again by hand is also slower than it needs to be.
How it works
$uri = new Uri\Rfc3986\Uri("https://example.com/foo/bar/baz"); $segments = $uri->getPathSegments(); // ["foo", "bar", "baz"] $uri = $uri->withPathSegments(["a", "b"]); echo $uri->getPath(); // /a/b
The same methods work on Uri\WhatWg\Url. withPathSegments() joins the parts with / and sets the result as the new path.
Empty segments count, so a trailing slash adds an empty segment at the end:
$uri = new Uri\Rfc3986\Uri("https://example.com/foo/"); $segments = $uri->getPathSegments(); // ["foo", ""]
Three ways to read segments
The RFC 3986 class gets three getters:
getRawPathSegments()returns the segments exactly as they appear.getPathSegments()returns normalized segments, the same waygetPath()does.getDecodedPathSegments()also percent-decodes sequences like%20back into characters, including%2F, which becomes/.
The WHATWG difference
The WHATWG URL standard only splits paths for special schemes like https. For other schemes, getPathSegments() returns the whole path as a string, so its return type is array|string.
Leading slashes
Take the relative path /foo and replace its segments with ["bar"]. Should the result be /bar or bar? The RFC adds a LeadingSlashPolicy enum to decide. AddForNonEmptyRelative is the default and adds the slash, while NeverAdd leaves it off. WHATWG URLs don't allow relative paths, so they don't need this option.
The RFC uses plain arrays rather than a new PathSegments class. It argues that a class would raise hard questions about when the segments should be validated.
What it means for existing code
Nothing breaks. The RFC only adds new methods and a new enum.
Where it stands
The RFC is a draft, first written in April 2026. It targets PHP 8.6, and the planned vote needs a two-thirds majority.