Jump to content

Archived

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

Meza44

PHP and phone numbers

Recommended Posts

I am creating a phone directory database and I don't want to have to hand format each phone number is there a way for php to either format it before inserting it into the database or formatting it when calling it from the db in the format (555)555-5555.

thanks for all your help I am so green with php that I am lost most times.

Meza44

Share this post


Link to post
Share on other sites
try

[code]
<?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;
}

echo format_template ('1234567890', '(###) ###-####');
echo '<br />';

// or even   
echo format_template ('12345678901234', '(###) ###-#### ext ####');   
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.