探讨PHP函数ip2long转换IP时数值太大产生负数的解决方法

yipeiwu_com6年前PHP代码库

【造成原因】:Because PHP's integer type is signed, and many IP addresses will result in negative integers.
【解决办法】:其官方手册中提到,可以“you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned IP address”
即,printf( '%u', ip2long( 'IP地址' ) );
或者将其先转换为二进制然后在转换为十进制,bindec( decbin( ip2long( 'IP地址' ) ) );
【测试】
$strIp = '182.118.0.0';

echo ip2long($strIp); //此时输出的-1233780736
echo '<br/>';
echo bindec( decbin( ip2long( $strIp ) ) ); // 输出3061186560,与MySQL函数输出一致~

【注】:
number bindec ( string $binary_string ); //二进制转换为十进制
string decbin ( int $number ); //十进制转换为二进制

以上这篇探讨PHP函数ip2long转换IP时数值太大产生负数的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

PHP随机字符串生成代码(包括大小写字母)

第一种:利用字符串函数操作 复制代码 代码如下: <?php function createRandomStr($length){ $str = '0123456789abcdef...

PHP遍历数组的三种方法及效率对比分析

本文实例分析了PHP遍历数组的三种方法及效率对比。分享给大家供大家参考。具体分析如下: 今天有个朋友问我一个问题php遍历数组的方法,告诉她了几个。顺便写个文章总结下,如果总结不全还请朋...

PHP-Fcgi下PHP的执行时间设置方法

一般情况下设置PHP脚本执行超时的时间 一、在php.ini里面设置 max_execution_time = 1800; 二、通过PHP的ini_set 函数设置 ini_set("m...

PHP利用REFERER根居访问来地址进行页面跳转

比如,我有一个开发一个黄页源码上转到了【宜配屋www.yipeiwu.com】。之前定了一个演示程序地址: //www.jb51.net 而现在这个域名需要用来作其它的站,又不杀望原来的...

PHP学习笔记(一):基本语法之标记、空白、和注释

一、PHP 标记 1、XML风格 复制代码 代码如下: <?php echo "hello world";?> 2、简短风格 复制代码 代码如下: <...