Jump to content

Convert single integer to double digit values


EchoFool

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.