note 109298 added to function.ip2long

From: Date: Wed, 04 Jul 2012 02:42:52 +0000
Subject: note 109298 added to function.ip2long
Groups: php.notes 
Request: Send a blank email to php-notes+get-189022@lists.php.net to get a copy of this message
<?php /** * *get the first ip and last ip from cidr(network id and mask length) * i will integrate this function into "Rong Framework" :) * @author admin@wudimei.com * @param string $cidr 56.15.0.6/16 , [network id]/[mask length] * @return array $ipArray = array( 0 =>"first ip of the network", 1=>"last ip of the network" ); * Each element of $ipArray's type is long int,use long2ip( $ipArray[0] ) to convert it into ip string. * example: * list( $long_startIp , $long_endIp) = getIpRange( "56.15.0.6/16" ); * echo "start ip:" . long2ip( $long_startIp ); * echo "<br />"; * echo "end ip:" . long2ip( $long_endIp ); */ function getIpRang( $cidr) { list($ip, $mask) = explode('/', $cidr); $maskBinStr =str_repeat("1", $mask ) . str_repeat("0", 32-$mask ); //net mask binary string $inverseMaskBinStr = str_repeat("0", $mask ) . str_repeat("1", 32-$mask ); //inverse mask $ipLong = ip2long( $ip ); $ipMaskLong = bindec( $maskBinStr ); $inverseIpMaskLong = bindec( $inverseMaskBinStr ); $netWork = $ipLong & $ipMaskLong; $start = $netWork+1;//去掉网络号 ,ignore network ID(eg: 192.168.1.0) $end = ($netWork | $inverseIpMaskLong) -1 ; //去掉广播地址 ignore brocast IP(eg: 192.168.1.255) return array( $start, $end ); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 14.154.216.18 ---- Manual Page -- http://www.php.net/manual/en/function.ip2long.php Edit -- https://master.php.net/note/edit/109298 Del: integrated -- https://master.php.net/note/delete/109298/integrated Del: useless -- https://master.php.net/note/delete/109298/useless Del: bad code -- https://master.php.net/note/delete/109298/bad+code Del: spam -- https://master.php.net/note/delete/109298/spam Del: non-english -- https://master.php.net/note/delete/109298/non-english Del: in docs -- https://master.php.net/note/delete/109298/in+docs Del: other reasons-- https://master.php.net/note/delete/109298 Reject -- https://master.php.net/note/reject/109298 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#189022) next »