Jump to content

[SOLVED] 123456789 to 12 3456 7 89


willpower

Recommended Posts

here's a formatting function I wrote a while ago

<?php
function format_template($str, $template) {
    $str = str_replace(' ', '', $str);
    $kt = strlen($template);
    $ks = strlen($str);
    $res = '';
    $j = 0;
    for($i=0; $i<$kt; $i++) {
        if ($j==$ks) break;
        switch ($c = $template{$i}) {
            case '#':
                $res .= $str{$j++};
                break;
            case '!':
                $res .= strtoupper($str{$j++}) ;
                break;
            default:
                $res .= $c;
                break;
        }
    }
    return $res;
}

    /**
    * try it
    */
echo format_template ('0232892357', '## #### ####');                 // --> 02 3289 2357
echo '<br />';    
echo format_template ('12345678901234', '(###) ###-#### ext ####');  // --> (123) 456-7890 ext 1234  
echo '<br />';    
echo format_template ('07123456789', '+44 (#)### ### ####');        // -->  +44 (0) 7123 456 789 
echo '<br />';    
echo format_template ('ab123456x', '!! ## ## ## !');                // --> AB 12 34 56 X

?>

 

For your example

 

echo format_template ('123456789', '## #### # ##''); 

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.