手机
当前位置:查字典教程网 >编程开发 >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时数值太大产生负数的解决方法】相关文章:

php使用array_search函数实现数组查找的方法

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

Access数据库导入Mysql的方法之一

php以fastCGI的方式运行时文件系统权限问题及解决方法

PHP获取文件行数的方法

PHP下MAIL的另一解决方案

php对象和数组相互转换的方法

PHP中把有符号整型转换为无符号整型方法

PHP调用三种数据库的方法(3)

php实现根据IP地址获取其所在省市的方法

精品推荐
分类导航