EchoFool Posted November 1, 2010 Share Posted November 1, 2010 Hey, If i have a string which contains numbers and symbols... such as 1:2:4 (its not always : for the symbol it changes) Is there a function that can convert the numbers that are 9 or less to have a 0 infront of them such as: 01:02:04 (also this is not timestamp format - just normal strings) Quote Link to comment https://forums.phpfreaks.com/topic/217472-convert-single-integer-to-double-digit-values/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2010 Share Posted November 1, 2010 Either use str_pad or sprintf with a %02d format specifier. Quote Link to comment https://forums.phpfreaks.com/topic/217472-convert-single-integer-to-double-digit-values/#findComment-1129059 Share on other sites More sharing options...
EchoFool Posted November 1, 2010 Author Share Posted November 1, 2010 Thanks PFM Quote Link to comment https://forums.phpfreaks.com/topic/217472-convert-single-integer-to-double-digit-values/#findComment-1129064 Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2010 Share Posted November 1, 2010 Here is an example using preg_replace_callback (taken from the php.net example and modified to match groups of digits and to apply sprintf to pad with leading zeros) - <?php $line = "5:14:1+2+5+6+7"; $line = preg_replace_callback( '|\d+|', create_function( // single quotes are essential here, // or alternative escape all $ as \$ '$matches', 'return sprintf(\'%02d\',$matches[0]);' ), $line ); echo $line; ?> Quote Link to comment https://forums.phpfreaks.com/topic/217472-convert-single-integer-to-double-digit-values/#findComment-1129098 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.