Meza44 Posted October 29, 2006 Share Posted October 29, 2006 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 29, 2006 Share Posted October 29, 2006 try[code]<?phpfunction 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] Quote Link to comment Share on other sites More sharing options...
Meza44 Posted October 29, 2006 Author Share Posted October 29, 2006 thanks for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.