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', '## #### # ##''); 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.