手机
当前位置:查字典教程网 >编程开发 >php教程 >探讨PHP函数ip2long转换IP时数值太大产生负数的解决方法
探讨PHP函数ip2long转换IP时数值太大产生负数的解决方法
摘要:【造成原因】:BecausePHP'sintegertypeissigned,andmanyIPaddresseswillresultinn...

【造成原因】: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时数值太大产生负数的解决方法】相关文章:

thinkphp3.2中Lite文件替换框架入口文件或应用入口文件的方法

WordPress博客程序常见错误的解决方法

php函数重载的替代方法

PHP准确取得服务器IP地址的方法

php计算整个目录大小的方法

php恢复数组的key为数字序列的方法

php实现每天自动变换随机问候语的方法

php将图片文件转换成二进制输出的方法

PHP获取当前日期和时间及格式化方法参数

PHP生成唯一订单号的方法汇总

精品推荐
分类导航