Two PHP functions to convert to convert to and from numbers compatible to MySQL's INET_ATON and INET_NTOA.
<?php
/**
* Convert and IP address string to Long.
*
* Use these two functions to convert from and
* to numbers compatible to MySQLs INET_ATON and INET_NTOA
*
* @link http://www.php.net/manual/en/function.long2ip.php#107257
*
* @param string $ip
* @return int
*/
function convertIpToLong($ip)
{
return sprintf("%u", ip2long($ip));
}
/**
* Converts an IP address into it's string format representation.
*
* @param int $ip
* @return string
*/
function convertIpToString($ip)
{
$long = 4294967295 - ($ip - 1);
return long2ip(- $long);
}
$ip4addressStr = '74.125.239.9';
echo "IP4 address String: $ip4addressStr\n";
$ip4addressLong = convertIpToLong($ip4addressStr);
echo "IP4 address String to Long: $ip4addressLong\n";
$ip4addressLong2Str = convertIpToString($ip4addressLong);
echo "IP4 address Long to String: $ip4addressLong2Str\n";