phppup Posted June 3, 2019 Share Posted June 3, 2019 Is there a recommended BEST PRACTICE for storing (and retrieving) telephone numbers from a database table? Is it best to store (in the USA) the area code separate from the rest of the number? Remove parenthesis and/or dashes? Or is there no reason to concern myself with performance or hacking issues regarding the storage of phone numbers as 323-555-1212 or (323)555-1212 inside my table? Quote Link to comment https://forums.phpfreaks.com/topic/308790-best-way-to-store-telephone-numbers-in-database/ Share on other sites More sharing options...
gw1500se Posted June 3, 2019 Share Posted June 3, 2019 Use E.164 format. Quote Link to comment https://forums.phpfreaks.com/topic/308790-best-way-to-store-telephone-numbers-in-database/#findComment-1567287 Share on other sites More sharing options...
requinix Posted June 3, 2019 Share Posted June 3, 2019 Regarding formatting, like hyphens or parentheses or periods: Don't. Plus sign with the country code is fine, otherwise just a string of digits and you can format it on display however you want. Quote Link to comment https://forums.phpfreaks.com/topic/308790-best-way-to-store-telephone-numbers-in-database/#findComment-1567288 Share on other sites More sharing options...
ginerjm Posted June 3, 2019 Share Posted June 3, 2019 After reading thru the e164 post and still not understanding what it is saying I would suggest that the format that I have always used is close. Do not format the number with any parens or dashes or dots. Just the digits and save any formatting for when you need to output it. Quote Link to comment https://forums.phpfreaks.com/topic/308790-best-way-to-store-telephone-numbers-in-database/#findComment-1567289 Share on other sites More sharing options...
phppup Posted June 3, 2019 Author Share Posted June 3, 2019 Amy suggestions for best method of removing parentheses and hyphen before transferring into the database? Quote Link to comment https://forums.phpfreaks.com/topic/308790-best-way-to-store-telephone-numbers-in-database/#findComment-1567295 Share on other sites More sharing options...
Barand Posted June 3, 2019 Share Posted June 3, 2019 1. Don't put them in there in the first place. or 2. $phone = "(123) 456-7890" ; $justdigits = ''; for ($i=0, $k=strlen($phone); $i<$k; $i++) { $justdigits .= (ctype_digit($phone[$i])) ? $phone[$i] : ''; } echo $justdigits ; //--> 1234567890 When it comes to formatting the output I find a custom formatting function of use; $phone = '1234567890234' $format = '(###) ###-#### ext ###'; echo formatIt ($format, $phone); // -> (123) 456-7890 ext 234 function formatIt($format,$str) { $i = $j = 0; $res = ''; $kf = strlen($format); $str = str_replace(' ','',$str); $ks = strlen($str); while ($i < $kf && $j < $ks) { $res .= $format[$i]=='#' ? $str[$j++] : $format[$i]; ++$i; } if ($j<$ks) $res .= substr($str,$j); return $res; } Quote Link to comment https://forums.phpfreaks.com/topic/308790-best-way-to-store-telephone-numbers-in-database/#findComment-1567296 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.