Jump to content
phppup

Best way to store telephone numbers in database

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites

Amy suggestions for best method of removing parentheses and hyphen before transferring into the database?

 

Share this post


Link to post
Share on other sites

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;
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

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.