php教程

long2ip

(PHP 4, PHP 5, PHP 7)

long2ip将长整型转化为字符串形式带点的互联网标准格式地址(IPV4)

说明

long2ip ( int $proper_address ) : string

long2ip() 函数通过长整型的表达形式转化生成带点格式的互联网地址(例如:aaa.bbb.ccc.ddd )。

参数

proper_address

合格的地址,长整型的表达形式。

返回值

返回字符串的互联网 IP 地址。

更新日志

版本 说明
7.1.0 参数 proper_address 的类型从 string 改成 integer

注释

Note:

在 32 位架构中,从 string 转换 integer 整型形式的 ip 地址将有可能导致错误的结果,因为结果数字超出了 PHP_INT_MAX 限制。

参见

  • ip2long() - 将 IPV4 的字符串互联网协议转换成长整型数字

User Contributed Notes

steve at computurn dot com 30-Oct-2018 01:38
For a 32bit safe long2ip, which can accept string or signed integer input, try:

  function safelong2ip($long) {
    $binStr = sprintf("%032s", decbin((float)$long));
    if (strlen($binStr) != 32) {
      throw new Exception("Invalid IPv4 subnet!");
    }

    $ipArr = [];
    for ($i = 0; $i < 4; ++$i) {
      $ipArr[] = bindec(substr($binStr, $i*8, 8));
    }

    return implode('.', $ipArr);
  }
klawd at kamundo dot de 25-Jan-2012 10:34
Use these two functions to convert from and to numbers compatible to MySQLs INET_ATON and INET_NTOA
<?php
   
function convertIpToString($ip)
    {
       
$long = 4294967295 - ($ip - 1);
        return
long2ip(-$long);
    }
    function
convertIpToLong($ip)
    {
        return
sprintf("%u", ip2long($ip));
    }
?>
Anonymous 05-Aug-2011 09:38
If you're running a 32 bit machine you can get wrong IPs. To prevent from this just cast to float e.g.

<?php
long2Ip32bit
($ip){
   return
long2ip((float)$ip);
}
?>
php dot net at davidstockton dot com 03-Sep-2008 10:28
Beware when processing values that are invalid, you may get values that are different based on the OS.  For instance:

$ip = long2ip(pow(2,32)+1024);

On windows you get 255.255.255.255.  On linux it's 0.0.4.0.

So it seems it would be important to make sure the long you're converting is in the correct range.
Gabriel Malca 17-Mar-2006 05:01
If the function doesn't exist:

<?
    if (!function_exists("long2ip")) {
        function long2ip($long) {
            // Valid range: 0.0.0.0 -> 255.255.255.255
            if ($long < 0 || $long > 4294967295) return false;
            $ip = "";
            for ($i=3;$i>=0;$i--) {
                $ip .= (int)($long / pow(256,$i));
                $long -= (int)($long / pow(256,$i))*pow(256,$i);
                if ($i>0) $ip .= ".";
            }
            return $ip;
        }
    }
?>
flobee 13-Feb-2005 02:50
when importing ip-ranges to a mysql database using an INT(10) field - NOTE: that you will get problems when using intval() function!

copied from "cleong" : 02-Oct-2001 02:21
intval() handles overflow differently depending on the type of the argument.
<?php
// intval('10000000000') = 2147483647
// intval(1e10) = 1410065408
?>
Tom Crosley 16-Feb-2003 01:13
I wanted to be able to pass an IP address in a URL always as an unsigned int.  I then converted it back as shown below:

$ip_addr = "192.168.100.25";  // example IP address that converts to neg #

$s32int = ip2long($ip_addr);

$us32str = sprintf("%u",$s32int);               // convert to unsigned string

// display orig IP address, signed 32 bit version, unsigned 32 bit ver,
// finally converted back to IP addr

printf("%s ,%d, %s, %s", $ip_addr, $s32int, $us32str,
         long2ip(-(4294967296-$us32str)));

// tested on Linux/Apache PHP 4.1.2

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788