PHP 8.5 introduces a built-in URI extension for parsing and modifying URIs according to modern standards. It provides dedicated classes instead of relying only on string operations or parse_url().
Create an RFC 3986 URI
Example:
<?php
use Uri\Rfc3986\Uri;
$uri = new Uri(
'https://example.com/docs/page?lang=en'
);
echo $uri->getHost();
Output:
example.com
The URI object keeps the components structured, so you can read them without manually splitting strings.
Read URI Components
Example:
<?php
use Uri\Rfc3986\Uri;
$uri = new Uri(
'https://example.com:8443/products?id=15#details'
);
echo $uri->getScheme() . PHP_EOL;
echo $uri->getHost() . PHP_EOL;
echo $uri->getPort() . PHP_EOL;
echo $uri->getPath();
Component methods make code clearer when routing, validation, logging, or link rewriting depends on one part of a URI.
Modify a URI
The URI APIs use immutable-style operations: a modification returns a new object instead of unexpectedly changing the existing value.
Example:
<?php
use Uri\Rfc3986\Uri;
$original = new Uri('https://example.com/docs');
$updated = $original->withPath('/tutorials');
echo $updated->toString();
Output:
https://example.com/tutorials
RFC 3986 and WHATWG URLs
PHP 8.5 supports APIs for both RFC 3986 URIs and WHATWG-style URLs. Choose the model that matches the data you are handling rather than assuming every URI follows browser URL rules.
| Model | Typical use |
|---|---|
| RFC 3986 | General URI syntax and server-side identifiers |
| WHATWG URL | Browser-style URL parsing behavior |
Why Use the URI Extension?
- Component access is explicit.
- Parsing follows defined standards.
- Modification does not require fragile string replacement.
- Application code can distinguish URI models intentionally.
Do not treat successful parsing as proof that a remote URL is safe to request. Network security checks such as allowed hosts and private-address restrictions are separate concerns.
Conclusion
The PHP 8.5 URI extension gives applications structured, standards-aware URI handling. Use it when code needs to inspect or rewrite schemes, hosts, paths, queries, or other URI components reliably.