建立一个数组与范围的MAC地址 [英] Build an array with a range of MAC addresses

查看:387
本文介绍了建立一个数组与范围的MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立包含基于3个变量的MAC地址的完整列表数组:一个preFIX,开始和结束

I want to build an array which contains a full list of MAC addresses based on 3 variables: a prefix, a start, and an end.

让我们说我有 20:1E:50:3F:8A 为preFIX, 3E 作为一个开始,而 43 作为结束。这应该会生成MAC地址以下数组:

Let's say I have 20:1E:50:3F:8A as a prefix, 3E as a start, and 43 as an end. This should generate the following array of MAC addresses:

20:1E:50:3F:8A:3E
20:1E:50:3F:8A:3F
20:1E:50:3F:8A:40
20:1E:50:3F:8A:41
20:1E:50:3F:8A:42
20:1E:50:3F:8A:43

我开了一个头,但它不会做的工作,只是还没有,我不能让我的周围头:

I made a start, but it doesn't do the job just yet, and I can't get my head around it:

function generate($prefix, $start, $end){

    $start = str_split($start);
    $end = str_split($end);

    $start_first = $start[0];
    $start_second = $start[1];

    $end_first = $end[0];
    $end_second = $end[1];

    if(is_numeric($start_second)){
        $start_second_numeric = true;
    }
    else{
        $start_second_numeric = false;
    }

    if(is_numeric($end_second)){
        $end_second_numeric = true;
    }
    else{
        $end_second_numeric = false;
    }

    $mac_array = array();

    $range_first = range($start_first, $end_first);

    foreach($range_first as $first_character){

        if($start_second_numeric && $end_second_numeric || !$start_second_numeric && !$end_second_numeric){
            $range_second = range($start_second, $end_second);
        }
        elseif(!$start_second_numeric && $end_second_numeric){
            $range_second = range($start_second, "F");
            $range_third = range("0", $end_second);
        }
        elseif($start_second_numeric && !$end_second_numeric){
            $range_second = range($start_second, "9");
            $range_third = range("A", $end_second);
        }

        foreach($range_second as $second_character){
            $string = $prefix . ":" . $first_character . $second_character;
            array_push($mac_array, $string);
        }

        if(is_array($range_third)){
            foreach($range_third as $second_character){
                $string = $prefix . ":" . $first_character . $second_character;
                array_push($mac_array, $string);
            }
        }

    }

    return $mac_array;
}

有人能帮助我吗?

Can someone help me out?

推荐答案

运行范围应该是很容易的,假设你使用一个64位的能力PHP:

Running the range should be very easy, assuming you are using a 64bit capable PHP:

$mac_array = range(0x201E503F8A3E, 0x201E503F8A43);
foreach ($mac_array as $mac) {
    echo wordwrap(sprintf("%012X", $mac), 2, ":", true);
}

现在是怎么回事呢?

范围()函数创建一个包含整数从第一到第二个参数(如果你想改变的步骤,使用可选的第三个参数)数组

The range() function creates an array containing integer numbers from the first to the second parameter (if you want to change the steps between, use the optional third parameter).

我使用数字的十六进制写作,这也是这仅限于64位PHP的原因,因为MAC地址长度超过32位,48位为precise。

I use the hexadecimal writing of the numbers, and that's also the reason this is limited to 64bit PHP, because the MAC addresses are longer than 32 bits, 48 bits to be precise.

的foreach 在该整数数组进行迭代。

foreach iterates over that integer array.

现在每一个整数,一些格式的推移。 的sprintf()是格式字符串有用的 - 在这种情况下,我希望它打印一个十六进制数至少有12位数字,并用零填充。这就是字符串%012X一样。

Now for every integer, some formatting goes on. sprintf() is useful to format strings - in this case, I want it to print a hexadecimal number with at least 12 digits, and fill them with zeroes. That's what the string "%012X" does.

换行()是一个分割字符串成更小的单位,连字,默认情况下将一个字符串转换为75个字符或更少的几行,用换行连接的功能。我在这里滥用它有点十六进制数分成2个字符单位,并用一个冒号把它们联系起来。

wordwrap() is a function that splits strings into smaller units with linking characters, by default splits a string into several lines of 75 characters or less, linking with newlines. I abuse it here a bit to split the hexadecimal number into units of 2 characters, and linking them with a colon.

这一切都是建立到PHP已经和应该被使用。 :)

All this is build into PHP already, and should be used. :)

这篇关于建立一个数组与范围的MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆