PHP 8.5.0 Beta 1 available for testing

Voting

: min(two, four)?
(Example: nine)

The Note You're Voting On

spinyn at gmail dot com
16 years ago
Just want to add a comment to kaputt's valuable contribution to the task of matching hosts to ip ranges, efficiently. The script works fine if the binary representation of the ip involves no leading zeros. Unfortunately, the way decbin() seems to work, leading zeros in the binary representation of the first ip quad get dropped. That is a serious matter if you're trying to match all possible candidates in the checklist. In those cases the leading zeros need to be added back to get accurate matches for values in the first quad between 0-127 (or the binary equivalent, 0-01111111).

The solution I came up with to address this issue was the following function:

<?php
function addLeadingZero($ip) {
if ((
$result = (32 - strlen($ip))) > 0)
return
str_repeat("0", $result).$ip;
}
?>

<< Back to user notes page

To Top