Jump to content

Need Help Formating a Phone Number


vincej

Recommended Posts

Hi - I am storing phone numbers within the DB as a straight 10 digit int. ie  1112223333

 

I can search on them and it all works well. However I would like to make the output a little more friendly ie

 

111 222 3333

 

I have looked through the php arrays and there is nothing. I expect i need to use a regular expression, but I have so far failed miserably trying to make that work.

 

Any suggestions ?

 

Many Thanks !!

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/265329-need-help-formating-a-phone-number/
Share on other sites

Here is one that should work, although, it only takes the 10 digit format without any other characters (or it will break).

<?php

$pattern = '~([0-9]{3})([0-9]{3})([0-9]{4})~';
$replacement = '$1-$2-$3';
$number = 1112223333;

echo preg_replace($pattern,$replacement,$number);
//add parenthesis around the area code.
$replacement = '($1)-$2-$3';
echo preg_replace($pattern,$replacement,$number);
?>

@jcbones, since the value is only numbers then there is no need to the capture expressions to only be numbers. You could just as easily use

$pattern = '~(.{3})(.{3})(.{4})~';

 

However, in this case RegEx might be overkill since you can just use substr(). It might look like it's more complicated, but it's probably more efficient. Here's my two cents

function formatPhone($p)
{
    $phone = substr($p, 0, 3) . ' ' . substr($p, 3, 3) . ' ' . substr($p, 6);
    return $phone
}

@jcbones, since the value is only numbers then there is no need to the capture expressions to only be numbers. You could just as easily use

$pattern = '~(.{3})(.{3})(.{4})~';

 

However, in this case RegEx might be overkill since you can just use substr(). It might look like it's more complicated, but it's probably more efficient. Here's my two cents

function formatPhone($p)
{
    $phone = substr($p, 0, 3) . ' ' . substr($p, 3, 3) . ' ' . substr($p, 6);
    return $phone
}

 

You would think, but here is some benchmark'ing

Array

(

    [0] => Array

        (

            [Number_of_Loops_to_Perform_Test] => 10,000

            [Number_of_Tests_Performed] => 3

            [start_of_Test] => 07-06-2012 07:35:38pm

        )

 

    [Test: 1] => Array

        (

            [Code:] =>

$pattern = '~([0-9]{3})([0-9]{3})([0-9]{4})~';

$replacement = '($1)-$2-$3';

$number = 1112223333;

 

preg_replace($pattern,$replacement,$number);

            [start] => 1341617738.0938

            [End] => 1341617738.2981

            [Elapsed] => 0.2043

        )

 

    [Test: 2] => Array

        (

            [Code:] =>

$number = '1112223333';

if(!function_exists('formatPhone')) {

function formatPhone($p)

{

    $phone = substr($p, 0, 3) . ' ' . substr($p, 3, 3) . ' ' . substr($p, 6);

    return $phone;

}

}

formatPhone($number);

            [start] => 1341617738.2981

            [End] => 1341617738.7403

            [Elapsed] => 0.44218

        )

 

    [Test: 3] => Array

        (

            [Code:] =>

$number = '1112223333';

$phone = substr($number, 0, 3) . ' ' . substr($number, 3, 3) . ' ' . substr($number, 6);

            [start] => 1341617738.7403

            [End] => 1341617738.979

            [Elapsed] => 0.23869

        )

 

)

I did say " . . . might be overkill". But, I put mine into a function which is good practice.  And you added a function_exists() check which added even more overhead. When I reduced both methods down to the bare minimum my tests found the substr() solution to be faster.

 

These were the lines I ran for the tests which are as apples to apples as I could get

$value = preg_replace('~([0-9]{3})([0-9]{3})([0-9]{4})~', '($1)-$2-$3', $number);

 

$value = substr($number, 0, 3) . '-' . substr($number, 3, 3) . '-' . substr($number, 6);

 

Running each line 10,000 times with three runs each I got the following times:

 

First method:

Test 1: 0.025115966796875

Test 2: 0.025408029556274

Test 3: 0.025764942169189

 

Second method:

Test 1: 0.022403955459595

Test 2: 0.022188186645508

Test 3: 0.022217035293579

 

 

Pwnd by .003 seconds!  :happy-03:

Not as fast but definitely more flexible, here's a little utility function of mine

 

function formatIt($format,$str)
{
    $i = $j = 0;
    $res = '';
    $kf = strlen($format);
    $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;
}

$tel =  '01234567891234';
echo formatIt ('(###)###-#### ext ####', $tel );  //--> (012)345-6789 ext 1234
?>

You've triggered a pet peeve of mine so I'll make a quick point about something:

 

I am storing phone numbers within the DB as a straight 10 digit int

Phone numbers are not actually numbers. They are numeric but you can't, say, do math on them: 1112223333 + 1 is nonsensical, let alone something like division or modulus. It's blind luck that there aren't any 0XX or 00X area codes, otherwise that would screw things up.

They are strings which consist of numbers. Store them as strings.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.