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

yipeiwu_com5年前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获取linux命令结果的实例

如果使用php命令行里想获取etho网卡的IP怎么处理呢 ? public function get_server_ip() { if (PHP_SAPI === 'cli')...

二招解决php乱码问题

二招解决php乱码问题

php网页出现乱码一般是在建立数据库时用的编码和php网页的编码不同造成的, 用phpmyadmin建立的数据库如果你不指定编码他默认是latin1_swedish_ci 编码,既瑞典语...

解析PayPal支付接口的PHP开发方式

申请PayPal注册网址:https://www.paypal.com/ paypal接口与其它接口有些不同,稍微复杂一点。 其实银行接口也算是一个站点的插件。 所谓paypal ip...

php页面消耗内存过大的处理办法

解决办法: 1,修改 php.ini将memory_limit由 8M 改成 16M(或更大),重启apache服务 2,在PHP 文件中 加入 ini_set(”memory_limi...

PHP中usort在值相同时改变原始位置问题的解决方法

从 PHP 4.1.0 后,usort 在比较的值相同时,原始位置可能会改变,文档中是这样说的: If two members compare as equal, their order...