Generate string from 00:00:00 to FF:FF.FF in php

618 views Asked by At

I need to generate and echo values from 00:00:00 to FF:FF.FF like for counting from 1 to 1000000. I know with numbers but how with hexadecimal numbers? for example I need this at echo:

 00:00:00
 00:00:01
 00:00:02
 .
 .
 .
 FF:FF:00
 FF:FF:01
 FF:FF:02
 .
 .
 .
 FF:FF:FF

How to do this in PHP?

1

There are 1 answers

2
Mureinik On BEST ANSWER

PHP accepts integer literals in hexdecimal format, so you'd just be iterating from 0 to 0xFFFFFF. For each iteration, you could convert the integer to decimal format, left-pad it with zeroes and then split every to character and implode with : to get the format you want:

for ($i = 0; $i < 0xFFFFFF; ++$i) {
    echo implode(':', 
    str_split(str_pad(strtoupper(dechex($i)), 
      6, 
      '0', 
      STR_PAD_LEFT), 
    2)) . 
    '<br>';
}