final class IPAddress implements Stringable

Immutable IPv4/IPv6 address with predicates for range membership and address class.

Constants

private PrivateRanges

private LoopbackRanges

private LinkLocalRanges

private MulticastRanges

private ReservedRanges

Methods

__construct(string $address)

No description

static bool
isValid(string $address)

Returns true for any syntactically valid IPv4 or IPv6 address, including IPv4-mapped IPv6 (::ffff:1.2.3.4).

static IPAddress|null
tryFrom(string $address)

Returns an instance for valid input, null otherwise.

bool
isIPv4()

Returns true for IPv4 dotted-quad form. IPv4-mapped IPv6 returns false; see isIPv4Mapped().

bool
isIPv6()

Returns true for IPv6 form, including IPv4-mapped (::ffff:1.2.3.4).

bool
isIPv4Mapped()

Returns true for IPv4-mapped IPv6 (::ffff:a.b.c.d). Range predicates below normalize these — ::ffff:127.0.0.1 evaluates as loopback.

toIPv4()

Converts IPv4-mapped IPv6 to IPv4 dotted-quad form. Returns $this for non-mapped.

bool
isInRange(string $cidr)

Tests whether this IP falls within the given CIDR block.

bool
isPublic()

Returns true if address is publicly routable (not in any private, loopback, link-local, multicast or reserved range).

bool
isPrivate()

Tests RFC 1918 / RFC 4193 private ranges (10/8, 172.16/12, 192.168/16, fc00::/7).

bool
isLoopback()

Tests loopback ranges (127.0.0.0/8, ::1/128).

bool
isLinkLocal()

Tests link-local ranges (169.254.0.0/16 incl. cloud metadata 169.254.169.254, fe80::/10).

bool
isMulticast()

Tests multicast ranges (224.0.0.0/4, ff00::/8).

bool
isReserved()

Tests IANA-reserved ranges not covered by other predicates: documentation prefixes (192.0.2.0/24, 2001:db8::/32), CGNAT (100.64.0.0/10), benchmarking, future-use, unspecified (0.0.0.0/8, ::/128) and similar.

string
__toString()

No description

Details

at line 42
__construct(string $address)

No description

Parameters

string $address

Exceptions

InvalidArgumentException

at line 57
static bool isValid(string $address)

Returns true for any syntactically valid IPv4 or IPv6 address, including IPv4-mapped IPv6 (::ffff:1.2.3.4).

Parameters

string $address

Return Value

bool

at line 66
static IPAddress|null tryFrom(string $address)

Returns an instance for valid input, null otherwise.

Parameters

string $address

Return Value

IPAddress|null

at line 75
bool isIPv4()

Returns true for IPv4 dotted-quad form. IPv4-mapped IPv6 returns false; see isIPv4Mapped().

Return Value

bool

at line 84
bool isIPv6()

Returns true for IPv6 form, including IPv4-mapped (::ffff:1.2.3.4).

Return Value

bool

at line 94
bool isIPv4Mapped()

Returns true for IPv4-mapped IPv6 (::ffff:a.b.c.d). Range predicates below normalize these — ::ffff:127.0.0.1 evaluates as loopback.

Return Value

bool

at line 105
IPAddress toIPv4()

Converts IPv4-mapped IPv6 to IPv4 dotted-quad form. Returns $this for non-mapped.

Return Value

IPAddress

at line 123
bool isInRange(string $cidr)

Tests whether this IP falls within the given CIDR block.

Accepts '192.168.1.0/24' (network with prefix) or '192.168.1.1' (exact match, implicit /32 for IPv4 or /128 for IPv6). Returns false for malformed input or different IP family. IPv4-mapped IPv6 is normalized to IPv4 when the range is in IPv4 form.

Parameters

string $cidr

Return Value

bool

at line 172
bool isPublic()

Returns true if address is publicly routable (not in any private, loopback, link-local, multicast or reserved range).

Return Value

bool

at line 185
bool isPrivate()

Tests RFC 1918 / RFC 4193 private ranges (10/8, 172.16/12, 192.168/16, fc00::/7).

Return Value

bool

at line 194
bool isLoopback()

Tests loopback ranges (127.0.0.0/8, ::1/128).

Return Value

bool

at line 203
bool isLinkLocal()

Tests link-local ranges (169.254.0.0/16 incl. cloud metadata 169.254.169.254, fe80::/10).

Return Value

bool

at line 212
bool isMulticast()

Tests multicast ranges (224.0.0.0/4, ff00::/8).

Return Value

bool

at line 223
bool isReserved()

Tests IANA-reserved ranges not covered by other predicates: documentation prefixes (192.0.2.0/24, 2001:db8::/32), CGNAT (100.64.0.0/10), benchmarking, future-use, unspecified (0.0.0.0/8, ::/128) and similar.

Return Value

bool

at line 229
string __toString()

No description

Return Value

string