Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 4 years ago.
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.
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?