SDK for PHP V3

Rfc3986
in package

FinalYes

Syntax predicates and canonicalization helpers for the URI grammar defined by RFC 3986.

Table of Contents

Methods

canonicalizeIpv6()  : string
Returns the RFC 5952 canonical form of a valid IPv6 address.
isValidHost()  : bool
Whether the string is a valid URI host.
isValidPort()  : bool
Whether the string is a valid port number (0-65535).
isValidScheme()  : bool
Whether the string is a valid URI scheme.

Methods

canonicalizeIpv6()

Returns the RFC 5952 canonical form of a valid IPv6 address.

public static canonicalizeIpv6(string $address) : string

The address must be a valid textual IPv6 address without brackets and without a zone identifier, such as the inside of an IP-literal accepted by isValidHost(). Canonicalization lowercases the hexadecimal fields, suppresses leading zeros, and collapses the longest run of two or more zero fields (the leftmost on a tie) with ::. Embedded dotted-decimal notation follows the rendering policy of BIND-derived inet_ntop() implementations and curl: exactly the IPv4-mapped (::ffff:0:0/96) and deprecated IPv4-compatible (::/96) layouts use it, while other embedded-IPv4 forms, including translated (NAT64) well-known prefixes such as 64:ff9b::/96 (RFC 6052), serialize in pure hexadecimal fields.

Validation is strict and platform-independent: the address is checked against the RFC 3986 IPv6address grammar with PHP's FILTER_VALIDATE_IP filter and parsed in pure PHP, so spellings that only some platform parsers accept, such as the zero-padded dotted octets in ::ffff:192.168.001.001, are rejected everywhere.

Parameters
$address : string
Tags
throws
InvalidArgumentException

If the address cannot be parsed.

see
https://datatracker.ietf.org/doc/html/rfc5952#section-4
Return values
string

isValidHost()

Whether the string is a valid URI host.

public static isValidHost(string $host) : bool

Per RFC 3986 the host is IP-literal / IPv4address / reg-name. An empty host is accepted, since the authority (and thus the host) may be empty. Bracketed values are validated as IPv6 / IPvFuture literals; any other value is rejected if it contains control characters, whitespace, an authority or path delimiter (/ ? # @ \), an embedded colon denoting a port, a malformed percent-sequence, or a percent-encoded octet that decodes to one of those rejected bytes, to a bracket, or to % itself.

Parameters
$host : string
Tags
see
https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
Return values
bool

isValidPort()

Whether the string is a valid port number (0-65535).

public static isValidPort(string $port) : bool

RFC 3986 defines the port as *DIGIT, which also permits an empty port and has no upper bound. This applies the stricter policy used throughout the library instead: the value must be a non-empty run of digits (leading zeros are accepted and normalized) that resolves to 0-65535.

Parameters
$port : string
Tags
see
https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3
Return values
bool

isValidScheme()

Whether the string is a valid URI scheme.

public static isValidScheme(string $scheme) : bool

Per RFC 3986 a scheme must start with a letter, followed by letters, digits, +, -, or .. The empty string is also accepted, since a URI reference may omit the scheme.

Parameters
$scheme : string
Tags
see
https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
Return values
bool
<-- modeled_exceptions -->
On this page