# convert single digits to double digits in string

2 replies to this topic

### #1 vzwhaley

vzwhaley
• Members
• Member
• 14 posts

Posted 18 May 2006 - 05:44 PM

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.

```\$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);```

### #2 ober

ober
• Staff Alumni
• 5,337 posts
• LocationEast Coast, USA

Posted 18 May 2006 - 05:47 PM

```for(\$i=0;\$i<count(\$myarray);\$i++)
{
if(strlen(\$myarray[\$i]) == 1)
\$myarray[\$i] = "0" . \$myarray[\$i];
}
```

Info: PHP Manual

### #3 kenrbnsn

kenrbnsn
• Staff Alumni
• 8,235 posts
• LocationHillsborough, NJ, USA

Posted 18 May 2006 - 05:52 PM

Another method:
```<?php
for(\$i=0;\$i<count(\$myarray);\$i++)
if (strlen(\$myarray[\$i] == 1))
\$myarray[\$i] = sprintf("%02d",\$myarray[\$i]);
?>```

Ken

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users