Jump to content

Archived

This topic is now archived and is closed to further replies.

vzwhaley

convert single digits to double digits in string

Recommended Posts

Does anybody know of a good way to convert single digits, i.e. 1, 2, 3, 4, in a string to double double digits, i.e. 01, 02, 03, 04. I used the following code, but it places a 0 in front of numbers like 10, 11, 12, etc. Any help would be appreciated.

[code]
$Section = $temp[$i];
$search = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
$replace = array("01", "02", "03", "04", "05", "06", "07", "08", "09");
$Section3 = str_replace($search, $replace, $Section); [/code]

Share this post


Link to post
Share on other sites
[code]
for($i=0;$i<count($myarray);$i++)
{
    if(strlen($myarray[$i]) == 1)
       $myarray[$i] = "0" . $myarray[$i];
}
[/code]

Share this post


Link to post
Share on other sites
Another method:
[code]<?php
for($i=0;$i<count($myarray);$i++)
    if (strlen($myarray[$i] == 1))
       $myarray[$i] = sprintf("%02d",$myarray[$i]);
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.