note 119883 added to function.ip2long
| From: | alex at alex-at dot ru | Date: | Wed, 14 Sep 2016 08:52:09 +0000 |
| Subject: | note 119883 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206582@lists.php.net to get a copy of this message | ||
Here are my versions of prefix to mask and mask to prefix conversions. You can remove first lines
with if/preg of each function if you don't want sanity precheck.
function prefix2Mask($prefix)
{
if (!preg_match('#^(?:0*[0-2]?[0-9]|3[0-2])$#', $prefix)) return false; # wrong
prefix
return long2ip(0xffffffff << (32 - $prefix));
}
function mask2Prefix($mask)
{
if
(!preg_match('#^(?:(?:[0-1][0-9][0-9]|2[0-4][0-9]|25[0-5]|[0-9][0-9]|[0-9])\.){3}'.
'(?:[0-1][0-9][0-9]|2[0-4][0-9]|25[0-5]|[0-9][0-9]|[0-9])$#', $mask))
return false; # wrong mask
$m2p = str_pad(substr(decbin(ip2long($mask)), -32, 32), 32, '0', STR_PAD_LEFT);
if (!preg_match('#^(1*)0*$#', $m2p, $m)) return false; # wrong mask
return strlen($m[1]);
}
----
Server IP: 72.52.91.14
Probable Submitter: 212.71.0.254
----
Manual Page -- http://php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/119883
Del: integrated -- https://master.php.net/note/delete/119883/integrated
Del: useless -- https://master.php.net/note/delete/119883/useless
Del: bad code -- https://master.php.net/note/delete/119883/bad+code
Del: spam -- https://master.php.net/note/delete/119883/spam
Del: non-english -- https://master.php.net/note/delete/119883/non-english
Del: in docs -- https://master.php.net/note/delete/119883/in+docs
Del: other reasons-- https://master.php.net/note/delete/119883
Reject -- https://master.php.net/note/reject/119883
Search -- https://master.php.net/manage/user-notes.php