0

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.

4
  • echo json_encode($result); Commented Jun 17, 2018 at 14:31
  • instead of echo $result do a return $result;. and remember that result is an array so you have to convert it to a string before printing. Commented Jun 17, 2018 at 14:34
  • What are you expecting to be the result of the function - what is the CIDR you expect? Commented Jun 17, 2018 at 14:45
  • If you have a range of 8.8.8.1 to 8.8.8.10, the CIDR output should be: 8.8.8.1/32 8.8.8.2/31 8.8.8.4/30 8.8.8.8/31 8.8.8.10/32 Commented Jun 17, 2018 at 15:11

3 Answers 3

0

Let's return $result instead.

function iprange2cidr($ipStart, $ipEnd){
    ....
    return $result;
}

Then let's convert it to a string before we echo it:

$ipStart = '8.8.8.8';
$ipEnd = '8.8.8.254';
$range = iprange2cidr($ipStart, $ipEnd);
echo implode("\n",$range);
Sign up to request clarification or add additional context in comments.

1 Comment

I tried explode, it gave an error that 2nd paramater must be a string not an array. Then I tried "implode" and it worked: echo implode ("<br>",$range);
0

You can use print_r($result); to get human-readable output.

see doc for more info.

Comments

0

proper way of using function is to return value like

    function iprange2cidr($ipStart, $ipEnd){
....
return $result;}

and then call the function like $returnedVale = iprange2cidr($ipStart, $ipEnd);

$returnedVale = iprange2cidr($ipStart, $ipEnd);
echo"<pre>";print_r($returnedVale);echo"</pre>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.