note 56413 deleted from function.ip2long by cmb

From: Date: Sun, 18 Aug 2019 08:43:03 +0000
Subject: note 56413 deleted from function.ip2long by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211941@lists.php.net to get a copy of this message
Note Submitter: ---- <?php # m.ozarek # # Check if given mask is correct. You can check the short format mask # like 8,16,24 or long format like 255.255.255.0 # function isIpMask($mask){ $format = ''; if(preg_match("/[0-9]++\.[0-9]++\.[0-9]++\.[0-9]++/",$mask)){ $format = "long"; }else{ if($mask<=30){ $format = "short"; }else{ return false; } } switch($format){ case long; $mask = decbin(ip2long($mask)); break; case short: $tmp = $mask; for($i=0; $i < $mask ;$i++){ $tmp.= 1; } for($j=0; $j < (32 - $mask);$j++){ $tmp.= 0; } $mask = $tmp; break; } if(strlen($mask) <= 32){ for($i=0;$i<=32 ;$i++){ $bit = substr($mask,$i,1); if(($bit - substr($mask,$i+1,1)) < 0){ return false; } } } return true; } # EXAMPLE # isIpMask("255.255.255.0") -> return true # isIpMask("24") -> return true # isIpMask("5.5.5.5") -> return false ?> [EDIT BY danbrown AT php DOT net: Contains a fix provided by (greg AT netops DOT gvtc DOT com) on 19-Dec-2005.]

« previous php.notes (#211941) next »