Home  >  Article  >  php教程  >  ip地址和int相互转换

ip地址和int相互转换

WBOY
WBOYOriginal
2016-06-06 19:38:501568browse

ip地址和int相互转换 无 function ip_to_int($ip,$reverse=false){ if($reverse){ $ipVal = (int)$ip; $ipArr[0] = floor($ipVal / 0x1000000); $ipVint = $ipVal-($ipArr[0]*0x1000000); $ipArr[1] = ($ipVint 0xFF0000) 16; $ipArr[2] = ($ipVint 0xFF00 )

ip地址和int相互转换
function ip_to_int($ip,$reverse=false){
    if($reverse){
        $ipVal = (int)$ip;
        $ipArr[0] = floor($ipVal / 0x1000000);
        $ipVint   = $ipVal-($ipArr[0]*0x1000000);
        $ipArr[1] = ($ipVint & 0xFF0000)  >> 16;
        $ipArr[2] = ($ipVint & 0xFF00  )  >> 8;
        $ipArr[3] =  $ipVint & 0xFF;
        $ipDotted = implode('.', $ipArr);
        return $ipDotted;
    }

    $ipArr  =   explode('.',$ip);

    $ipInt  =   ($ipArr[0] << 24)
                + ($ipArr[1] << 16)
                + ($ipArr[2] << 8)
                + ($ipArr[3]);
    return $ipInt;

}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn