darsyn/ip
Laravel package for working with IP addresses: parse and validate IPv4/IPv6, compare and normalize formats, check ranges and subnet/CIDR matches, and convert between representations. Useful for request filtering, geo/security rules, and logging.
The IP value objects store their internal state as a binary string, which is not
easy for humans to understand. The Darsyn\IP\Util\Binary class is a collection
of static helper methods for dealing with such binary strings.
[@throws](https://github.com/throws) \InvalidArgumentException \Darsyn\IP\Util\Binary::fromHex(string $hex): string
Because there are two hexadecimal characters per byte, the input string must be of a length that is a multiple of 2.
<?php
use Darsyn\IP\Util\Binary;
$hexString = '48656c6c6f21';
Binary::fromHex($hexString); // string("Hello!")
[@throws](https://github.com/throws) \InvalidArgumentException \Darsyn\IP\Util\Binary::toHex(string $hex): string
<?php
use Darsyn\IP\Util\Binary;
$binaryString = 'Hello!';
Binary::toHex($binaryString); // string("48656c6c6f21")
[@throws](https://github.com/throws) \InvalidArgumentException \Darsyn\IP\Util\Binary::fromHumanReadable(string $asciiBinarySequence): string
Because there are 8 bits per bytes, the input string must be of a length that is a multiple of 8.
<?php
use Darsyn\IP\Util\Binary;
$asciiBinary = '010010000110010101101100011011000110111100100001';
Binary::fromHumanReadable($asciiBinary); // string("Hello!")
[@throws](https://github.com/throws) \InvalidArgumentException \Darsyn\IP\Util\Binary::toHumanReadable(string $binary): string
<?php
use Darsyn\IP\Util\Binary;
$binaryString = 'Hello!';
Binary::toHumanReadable($asciiBinary); // string("010010000110010101101100011011000110111100100001")
On some PHP installations, the Multibyte String extension can overload PHP's native string functions which, because we are working with arbitrary binary content, can incorrectly guess an encoding when attempting to transform this data (IP addresses have no text encoding).
Because of this, the Darsyn\IP\Util\MbString class is a collection of static
helper equivalents for PHP's core string functions which detect if the
mbstring extension is installed and appropriately call the correct function,
specifying the 8bit text encoding (treat each byte as individual character)
if required.
| Static Helper Method | Replaces the PHP function... |
|---|---|
MbString::getLength() |
str_len() |
MbString::subString() |
substr() |
MbString::padString() |
str_pad() |
How can I help you explore Laravel packages today?