探讨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通过ajax实现双击table修改内容

复制代码 代码如下: <script type="text/javascript"> $(function() { $("td").dblclick(function() {...

php导出excel格式数据问题

解决2个问题:1.身份证之类的文本数据自动转为科学计数法的问题。2.中文乱码的问题 excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以ex...

WordPress开发中用于获取近期文章的PHP函数使用解析

wp_get_recent_posts 函数在 WordPress 中是一个可以获取近期文章数组的函数,相较于其他的文章获取方式, wp_get_recent_posts 返回的将是一个...

解决php的“It is not safe to rely on the system’s timezone settings”问题

如果使用PHP5.3以上版本时,如果没有正确的配置php.ini就会出现PHP日期函数的错误。以前很多旧的PHP编程教程没有说到这个问题,所以不少读者会觉得困惑,下面笔者为大家讲述解决这...

使用 eAccelerator加速PHP代码的目的

使用 eAccelerator加速PHP代码  eAccelerator 真是一个好东西(它的前身是truck-mmcache)。  简单来讲它是...