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 Link to comment https://forums.phpfreaks.com/topic/25466-php-and-phone-numbers/ 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] Link to comment https://forums.phpfreaks.com/topic/25466-php-and-phone-numbers/#findComment-116211 Share on other sites More sharing options...
Meza44 Posted October 29, 2006 Author Share Posted October 29, 2006 thanks for the help. Link to comment https://forums.phpfreaks.com/topic/25466-php-and-phone-numbers/#findComment-116217 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.