How to Convert a Number to single digits

I am trying to come up with an elegant way to convert a number to single digits. I will use these single digits to show a nice graphic image of the number. The number will be small in the start, such as 100, and then it will climb to 1 million and probably not much further than that but it could go further. Can anyone provide any different methods or if my method is terrible? Or even different logic for the concept would be cool.

Here is what I have so far:


$number = "12,345,689";
echo $number."<br><br/>";

$length = strlen($number);
$digits = array();
$i=1;
while($length >= $i){
	array_push($digits,substr($number, -$i,1));
$i++;
}
$digits = array_reverse($digits);
print_r($digits);

// results in
Array
(
    [0] => 1
    [1] => 2
    [2] => ,
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => ,
    [7] => 6
    [8] => 8
    [9] => 9
)


Use this for one-step work:

$fancy_images = preg_replace(‘/([\d])/’, ‘<img src=“fancy/$1.jpg”>’, preg_replace(‘/[^\d]+/’, ‘’, $number));
echo $fancy_images;

It removes non-digits.
It loads images for each digits under image directory - facny/, for .jpg files.

[fphp]str_split/fphp :wink:

Awesome another great solution! Thanks a ton!

Lol wow. I read through so many different functions but missed that one lol. I like to re-invent the wheel when I can hehe. Jk.

Thanks!