ip2long的用法

ip2long:将IPv4的ip地址(以小数点分隔形式)转换为int

Description

int ip2long ( string ip_address )

如果ip地址非法,返回FALSE(PHP 5.0.0以后)

注意,由于返回值是int,所以会造成数据溢出,比如有些ip地址转换后变成负数,因此需要转换成unsigned int类型

unsigned int _ip2long( string ip_address )

function _ip2long( $ip_address )
{
    return sprintf("%u",ip2long($ip_address));
}

如果要确认一个ip地址是否合法(IP validation ),最好的使用方式是结合long2ip()函数一起使用

例如:

<?php
    // make sure IPs are valid. also converts a non-complete IP into
    // a proper dotted quad as explained below.
    $ip = long2ip ( ip2long ( "127.0.0.1" )); // "127.0.0.1"
    $ip = long2ip ( ip2long ( "10.0.0" ));    // "10.0.0.0"
    $ip = long2ip ( ip2long ( "10.0.256" ));  // "10.0.1.0"

原文地址:https://www.cnblogs.com/tl542475736/p/4104151.html