ip2long()
Run Code Online (Sandbox Code Playgroud)
http://php.net/manual/en/function.ip2long.php
由于您说要将其与MySQL数据库一起使用,因此请使用这两个函数来转换为与MySQL INET_ATON和INET_NTOA兼容的数字.
<?php
function convertIpToString($ip)
{
$long = 4294967295 - ($ip - 1);
return long2ip(-$long);
}
function convertIpToLong($ip)
{
return sprintf("%u", ip2long($ip));
}
?>
Run Code Online (Sandbox Code Playgroud)
参考MySQL函数:
INET_ATON() -- Return the numeric value of an IP address
INET_NTOA() -- Return the IP address from a numeric value
Run Code Online (Sandbox Code Playgroud)
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton