这问题确实有点难,不过实际应用性很强啊,呵呵。我整理了一下,给你一个示例吧,应该对你很有帮助:- <?php
- function mask2bin( $n ) {
- $n = intval($n);
- if ( $n < 0 || $n > 32 ) die( 'error submask ');
- return str_repeat( "1 ", $n).str_repeat( "0 ",32-$n);
- }
- function revBin( $s ) {
- $p = array( '0 ', '1 ', '2 ' );
- $r = array( '2 ', '0 ', '1 ' );
- return str_replace( $p, $r, $s );
- }
- function execIp( $str ) {
- list($ip, $m) = explode( "/ ", $str);
- $bIp = decbin( ip2long($ip) );
- $bSub = mask2bin(20);
- $sIp = $bIp & $bSub;
- $eIp = $bIp | revBin($bSub);
- print " ip : ".$sIp. " - ".$eIp;
- }
- $str = "192.168.1.5/20 ";
- execIp( $str );
- ?>
复制代码 输出结果是:- ip : 0 - 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
复制代码 |