Jump to content


Photo

PHP and phone numbers


  • Please log in to reply
2 replies to this topic

#1 Meza44

Meza44
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 29 October 2006 - 09:38 AM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 29 October 2006 - 10:25 AM

try

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Meza44

Meza44
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 29 October 2006 - 11:05 AM

thanks for the help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users