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

 

 

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 :)

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.