vzwhaley Posted May 18, 2006 Share Posted May 18, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/9941-convert-single-digits-to-double-digits-in-string/ Share on other sites More sharing options...
ober Posted May 18, 2006 Share Posted May 18, 2006 [code]for($i=0;$i<count($myarray);$i++){ if(strlen($myarray[$i]) == 1) $myarray[$i] = "0" . $myarray[$i];}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9941-convert-single-digits-to-double-digits-in-string/#findComment-36947 Share on other sites More sharing options...
kenrbnsn Posted May 18, 2006 Share Posted May 18, 2006 Another method:[code]<?phpfor($i=0;$i<count($myarray);$i++) if (strlen($myarray[$i] == 1)) $myarray[$i] = sprintf("%02d",$myarray[$i]);?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/9941-convert-single-digits-to-double-digits-in-string/#findComment-36950 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.