PHP Classes

PHP Convert IPv4 Address: Convert formats of IPv4 network addresses

Recommend this page to a friend!
  Info   View files Documentation   View files View files (28)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2021-05-17 (6 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 27 All time: 10,381 This week: 282Up
Version License PHP version Categories
ipv4-address-convert 1.0Custom (specified...5Networking, PHP 5, Conversion
Description Author

This package can convert formats of IPv4 network addresses.

It can take a string with a IPv4 address and converts to other formats like numbers in decimal, hexadecimal and binary.

The class can also do the opposite, i.e. take a IPv4 address as numbver in a decimal, hexadecimal and binary.

Picture of Angel Campos
Name: Angel Campos <contact>
Classes: 6 packages by
Country: Spain Spain
Age: ???
All time rank: 4081110 in Spain Spain
Week rank: 151 Up9 in Spain Spain Up
Innovation award
Innovation award
Nominee: 2x

Details

IPv4 Address Converter

License Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality StyleCI Total Downloads

This PHP package allows you to perform IPv4 Address conversion within Laravel applications.

Features

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

Conversions

Converts from:

  • Binary
  • Decimal
  • Hexadecimal
  • Long

Converts to:

  • Binary
  • Decimal
  • Hexadecimal
  • Long

Installation

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.

Usage

Input Methods

These are the valid input methods for the conversion of the IP Addresses.

fromBinary

Set the input for the IP Address conversion as a binary string.

| accepts | string $address | | --- | --- | | returns | IPv4AddressConverter |

fromDecimal

Set the input for the IP Address conversion as a doted decimal string.

| accepts | string $address | | --- | --- | | returns | IPv4AddressConverter |

fromHexadecimal

Set the input for the IP Address conversion as a hexadecimal string.

| accepts | string $address | | --- | --- | | returns | IPv4AddressConverter |

fromLong

Set the input for the IP Address conversion as a long integer.

| accepts | string $address | | --- | --- | | returns | IPv4AddressConverter |

Output Methods

These methods are valid output methods for the conversion of the IP address specified in the previous input methods.

toBinary

Set binary string as desired output format.

toDecimal

Set dotted decimal as desired output format.

toHexadecimal

Set hexadecimal string as desired output format.

toLong

Set long integer as desired output format.

Modifiers

With these modifiers we can control the output of the conversion operation.

withDotNotation

This modifier will apply dot notation to the output of the conversion, only available to binary strings and hexadecimal strings.

Sample outputs

From Decimal to Long Integer

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)

From Decimal to Binary String

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"

From Decimal to Binary String with Dot Notation

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"

From Decimal to Hexadecimal

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"

From Decimal to Hexadecimal with Dot Notation

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"

Output All

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)
}

Testing

To run the tests you only need to run this command:

composer test

Changelog

Please see CHANGELOG.md for more information what has changed recently.

Contributing

Thank you for considering contributing to the improvement of the package. Please see CONTRIBUTING.md for details.

Security Vulnerabilities

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.

Standards

The php package IPv4 Address Converter, comply with the next standards:

Credits

Angel Campos

License

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.

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file, 7 directories)
Files folder imagetests (6 files)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageConverters (5 files)
Files folder imageEnums (1 file)
Files folder imageExceptions (1 file)
Files folder imageFacades (1 file)
Files folder imageInterfaces (1 file)
Files folder imageServiceProviders (1 file)
Files folder imageTraits (1 file)
  Plain text file IPv4AddressConverter.php Class Class source

  Files folder image Files  /  src  /  Converters  
File Role Description
  Plain text file BaseAddressConverter.php Class Class source
  Plain text file BinaryIPv4AddressConverter.php Class Class source
  Plain text file DecimalIPv4AddressConverter.php Class Class source
  Plain text file HexadecimalIPv4AddressConverter.php Class Class source
  Plain text file LongIPv4AddressConverter.php Class Class source

  Files folder image Files  /  src  /  Enums  
File Role Description
  Plain text file IPAddressFormatEnum.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file OutputFormatException.php Class Class source

  Files folder image Files  /  src  /  Facades  
File Role Description
  Plain text file IPv4AddressConverterFacade.php Class Class source

  Files folder image Files  /  src  /  Interfaces  
File Role Description
  Plain text file IPv4AddressConverterInterface.php Class Class source

  Files folder image Files  /  src  /  ServiceProviders  
File Role Description
  Plain text file IPv4AddressConverterServiceProvider.php Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
  Plain text file MutableIPv4AddressTrait.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file BaseTestCase.php Class Class source
  Plain text file GetAddressesFromBinaryStringTest.php Class Class source
  Plain text file GetAddressesFromDecimalStringTest.php Class Class source
  Plain text file GetAddressesFromHe...cimalStringTest.php Class Class source
  Plain text file GetAddressesFromLongIntegerTest.php Class Class source
  Plain text file InstanceTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:27
This week:0
All time:10,381
This week:282Up
For more information send a message to info at phpclasses dot org.