This PHP package allows you to perform IPv4 Address conversion within Laravel applications.
- Features
- Installation
- Usage
- Sample outputs
- Testing
- Changelog
- Contributing
- Security Vulnerabilities
- Standards
- Credits
- License
The package accepts an IP address as an input and converts it to a specified IP address format.
The input and output can be one of these formats: binary string, dotted decimal, hexadecimal string, or long integer
Converts from:
- Binary
- Decimal
- Hexadecimal
- Long
Converts to:
- Binary
- Decimal
- Hexadecimal
- Long
You can install the package via composer and then publish the assets:
Add the library to your composer.json file:
{
"require": {
"acamposm/ipv4-address-converter": "^1.0"
}
}
Or use composer to install the library:
composer require acamposm/ipv4-address-converter
Note: We try to follow SemVer v2.0.0.
These are the valid input methods for the conversion of the IP Addresses.
Set the input for the IP Address conversion as a binary string.
accepts | string $address |
---|---|
returns | IPv4AddressConverter |
Set the input for the IP Address conversion as a doted decimal string.
accepts | string $address |
---|---|
returns | IPv4AddressConverter |
Set the input for the IP Address conversion as a hexadecimal string.
accepts | string $address |
---|---|
returns | IPv4AddressConverter |
Set the input for the IP Address conversion as a long integer.
accepts | string $address |
---|---|
returns | IPv4AddressConverter |
These methods are valid output methods for the conversion of the IP address specified in the previous input methods.
Set binary string as desired output format.
Set dotted decimal as desired output format.
Set hexadecimal string as desired output format.
Set long integer as desired output format.
With these modifiers we can control the output of the conversion operation.
This modifier will apply dot notation to the output of the conversion, only available to binary strings and hexadecimal strings.
This example shows how to convert a dotted-decimal IP address to a long integer IP address.
use Acamposm\IPv4AddressConverter\IPv4AddressConverter;
$converter = IPv4AddressConverter::convert()
->fromDecimal('192.168.10.254')
->toLong()
->get();
var_dump($converter);
The output of the conversion is a integer.
int(3232238334)
This example shows how to convert a dotted decimal IP address to binary string IP address.
use Acamposm\IPv4AddressConverter\IPv4AddressConverter;
$converter = IPv4AddressConverter::convert()
->fromDecimal('192.168.10.254')
->toBinary()
->get();
var_dump($converter);
The output is a binary string IP Address.
string(32) "11000000101010000000101011111110"
This example shows how to convert a dotted-decimal IP address to binary string IP address with dot notation.
use Acamposm\IPv4AddressConverter\IPv4AddressConverter;
$converter = IPv4AddressConverter::convert()
->fromDecimal('192.168.10.254')
->toBinary()
->withDotNotation()
->get();
var_dump($converter);
The output is a binary string IP Address with dot notation.
string(35) "11000000.10101000.00001010.11111110"
This example shows how to convert a dotted-decimal IP address to a hexadecimal string IP address.
use Acamposm\IPv4AddressConverter\IPv4AddressConverter;
$converter = IPv4AddressConverter::convert()
->fromDecimal('192.168.10.254')
->toHexadecimal()
->get();
var_dump($converter);
The output of the conversion is a hexadecimal string IP address.
string(8) "C0A80AFE"
This example shows how to convert a dotted-decimal IP address to a hexadecimal string IP address with dot notation.
use Acamposm\IPv4AddressConverter\IPv4AddressConverter;
$converter = IPv4AddressConverter::convert()
->fromDecimal('192.168.10.254')
->toHexadecimal()
->withDotNotation()
->get();
var_dump($converter);
The output of the conversion is a hexadecimal string IP address with dot notation.
string(11) "C0.A8.0A.FE"
There's an all method, that converts an input address to all formats.
The input address for the conversion can be a binary, decimal, hexadecimal or long address.
use Acamposm\IPv4AddressConverter\IPv4AddressConverter;
$converter = IPv4AddressConverter::convert()
->fromDecimal('192.168.10.254')
->withDotNotation()
->all();
var_dump($converter);
The output is an object with the address converted to all formats.
object(stdClass)#638 (4) {
["binary"] =>
string(35) "11000000.10101000.00001010.11111110"
["decimal"] =>
string(14) "192.168.10.254"
["hexadecimal"] =>
string(11) "C0.A8.0A.FE"
["long"] =>
int(3232238334)
}
To run the tests you only need to run this command:
composer test
Please see CHANGELOG.md for more information what has changed recently.
Thank you for considering contributing to the improvement of the package. Please see CONTRIBUTING.md for details.
If you discover any security related issues, please send an e-mail to Angel Campos via angel.campos.m@outlook.com instead of using the issue tracker. All security vulnerabilities will be promptly addressed.
The php package IPv4 Address Converter, comply with the next standards:
Angel Campos
The package IPv4 Address Converter is an open-source package and is licensed under The MIT License (MIT). Please see License File for more information.