Jump to content

How to change format from 8005551212 to (800)555-1212


ghurty

Recommended Posts

I am trying to convert a variable that contains a telephone number in  this format: 8005551212  to this format:(800) 555-1212.

 

 

The variable that contains the number is  $cidnum:

 

$callerName = getCallerInfo($cidnum, $adb);

 

So I would want to add a line above it that will switch $cidnum from being 8005551212 to (800) 555-1212.

 

 

That is what I would mainly like to do. But is there a way to also have it that if it is a longer number (ie: a international number: 0845 060 3000) To have it that in that case it will rewrite it to be the international style?

 

Thanks

Thanks

 

 

Link to comment
Share on other sites

function formatCaller($callerName){

switch strlen($callerName)
$callerName=strval($callerName);
case 7:
return substr($callerName,0,3)."-".substr($callerName,3);
case 10:
return "(".substr($callerName,0,3).") ".formatCaller(substr(3));
case 11:
return substr($callerName,0,4)." ".substr($callerName,4,3)." ".substr($callerName,7);
default:
return FALSE;
}

echo formatCaller(5551234);                  //prints 555-1234
echo formatCaller(5551231234);            //prints (555) 123-1234
echo formatCaller(12341231234);         // prints 1234 123 1234

I couldn't help but use the self calling syntax for case 10 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.