Skip to content

Core\Validate::isIp

Core\Validate::isIp(string $s, {version?: 4|6}): boolCore\Validate::isIp(
    string $s,
    {version?: 4|6},
): bool

Checks whether a text is an IP address. An IP address is the number of a computer on a network. The method returns true or false.

There are two versions of IP addresses. An IPv4 address is four numbers from 0 to 255 joined by dots, such as 192.0.2.1. An IPv6 address is up to eight groups of hex digits joined by :, such as 2001:db8::1. A :: replaces a run of groups that are all zero.

Without options, both versions are accepted. The option version set to 4 accepts only IPv4, and set to 6 accepts only IPv6.

A number with a leading zero, such as 192.0.2.01, gives false. So does a space around the address, or a zone at the end, such as fe80::1%eth0. The method does not check whether the address exists.

The examples below check a few texts, accept only one version, and check an allow list that somebody typed in.

Parameters

  • $sstringThe text to test.

Options

  • version4|6optional4 accepts only IPv4 addresses, and 6 accepts only IPv6 addresses. Without it, both are accepted.

Return value

Returns bool. true for an IP address of the chosen version. false for anything else. A number with a leading zero, such as 192.0.2.01, gives false. An IPv6 address with a zone at the end, such as fe80::1%eth0, also gives false.

Examples

Check a few texts

<?nvs
// Shows which texts are IP addresses.
// "256.0.0.1" has a number over 255, and "192.0.2.01" has a number with a leading zero.
// "example.com" is a domain name, not an IP address.
array<string> $texts = ['192.0.2.1', '2001:db8::1', '256.0.0.1', '192.0.2.01', 'example.com'];
// `Core\Validate::isIp` returns `true` for an IPv4 or an IPv6 address.
// This prints yes for the first two texts and no for the other three.
foreach ($texts as string $text) {
echo $text, ': ', Core\Validate::isIp($text) ? 'yes' : 'no', "\n";
}
Output
192.0.2.1: yes
2001:db8::1: yes
256.0.0.1: no
192.0.2.01: no
example.com: no

Accept only one version

<?nvs
// Shows how to accept only IPv4 addresses or only IPv6 addresses.
// "::ffff:192.0.2.1" contains an IPv4 address, but it is written as an IPv6 address.
array<string> $addresses = ['192.0.2.1', '2001:db8::1', '::ffff:192.0.2.1'];
// `{ version: 4 }` accepts only IPv4, and `{ version: 6 }` accepts only IPv6.
// This prints "4 yes, 6 no" for the first address and "4 no, 6 yes" for the other two.
foreach ($addresses as string $address) {
echo $address, ': 4 ', Core\Validate::isIp($address, { version: 4 }) ? 'yes' : 'no';
echo ', 6 ', Core\Validate::isIp($address, { version: 6 }) ? 'yes' : 'no', "\n";
}
Output
192.0.2.1: 4 yes, 6 no
2001:db8::1: 4 no, 6 yes
::ffff:192.0.2.1: 4 no, 6 yes

Check an allow list

<?nvs
// Shows how to check an allow list of IP addresses before a program saves it.
// Each line is an address that somebody typed into a settings form.
array<string> $lines = ['192.0.2.10', ' 198.51.100.7 ', '10.0.0.300', '2001:db8::42', 'localhost'];
// `trim` removes the spaces around each line. Only real IP addresses go into the list.
// "10.0.0.300" has a number over 255, and "localhost" is a name.
array<string> $allowed = [];
foreach ($lines as string $line) {
string $address = Core\Str::trim($line);
if (Core\Validate::isIp($address)) {
$allowed[] = $address;
} else {
echo 'skipped: ', $address, "\n";
}
}
// This prints 3 and then the three saved addresses.
echo 'saved ', Core\Arr::count($allowed), ': ', Core\Str::join($allowed, ', '), "\n";
Output
skipped: 10.0.0.300
skipped: localhost
saved 3: 192.0.2.10, 198.51.100.7, 2001:db8::42