I'm having problems trying to "print" a PHP function to convert an IP address range to CIDR format, here is the function posted by IP2Location.com :
https://www.ip2location.com/tutorials/how-to-convert-ip-address-range-into-cidr
function iprange2cidr($ipStart, $ipEnd){
if (is_string($ipStart) || is_string($ipEnd)){
$start = ip2long($ipStart);
$end = ip2long($ipEnd);
}
else{
$start = $ipStart;
$end = $ipEnd;
}
$result = array();
while($end >= $start){
$maxSize = 32;
while ($maxSize > 0){
$mask = hexdec(iMask($maxSize - 1));
$maskBase = $start & $mask;
if($maskBase != $start) break;
$maxSize--;
}
$x = log($end - $start + 1)/log(2);
$maxDiff = floor(32 - floor($x));
if($maxSize < $maxDiff){
$maxSize = $maxDiff;
}
$ip = long2ip($start);
array_push($result, "$ip/$maxSize");
$start += pow(2, (32-$maxSize));
}
return $result;
}
function iMask($s){
return base_convert((pow(2, 32) - pow(2, (32-$s))), 10, 16);
}
(note: corrected 'echo' to 'return' result)
I've tried all of the suggested ways of "feeding" the $ipStart and $ipEnd values to the function, and also to "echo" or "print" the resulting array, but all I get is the word "Array".
For example, after the function is defined, I try:
$ipStart = '8.8.8.8';
$ipEnd = '8.8.8.254';
echo iprange2cidr($ipStart, $ipEnd);
... I appologise for the novice question, I'm a PHP newbie. I'm just not sure how to use the function. Any guidance on what I'm doing wrong would be appreciated! My server uses PHP 7.1. Thank you.
echo json_encode($result);echo $resultdo areturn $result;. and remember that result is an array so you have to convert it to a string before printing.