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) 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. 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 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; ?> 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
Archived
This topic is now archived and is closed to further replies.