hi,欢迎访问本站!
当前位置: 首页编程开发正文

php整型转换为ip的方法

墨初 编程开发 379阅读

上篇博文写了一下在php脚本将IP地址转为十进制的方法,那这篇博文73so博客就说一下如果将十进制的IP地址转为标准的IP地址的方法。

php整型转为IP地址的方法

方法1:

/**
 * # 十进制IP转标准IP的方法
 * @param string $IPNum 十进制的IP地址
 * 
 * @return string
 * @host https://www.73so.com
 */
function Long2DotIP ($IPNum) 
{
return $IPNum == "" ? '0.0.0.0' : (($IPNum / 16777216) % 256) . "." . (($IPNum / 65536) % 256) . "." . (($IPNum / 256) % 256) . "." . ($IPNum % 256);
}
echo Long2DotIP('3232235521');
// 192.168.0.1

方法2:

/**
 * # 十进制IP转标准IP的方法
 * @param string $IPNum 十进制的IP地址
 * 
 * @return string
 * @host https://www.73so.com
 */
function intToIP($iIP){
    $xor = array(0x000000ff,0x0000ff00,0x00ff0000,0xff000000);
    for($i=0; $i<4; $i++){
        $s = '';
        $s = ($iIP & $xor[$i]) >> $i*8;
        if ($s < 0) $s += 256;
        $ips[] = $s;
    }
    krsort($ips);
    return implode('.',$ips);
}
echo intToIP('3232235521');
// 192.168.0.1

上面就是php中有关整数的IP地址转为标准IP地址的方法,各位可以参考一下。

声明:无特别说明,转载请标明本文来源!
相关推荐