ghurty Posted March 29, 2010 Share Posted March 29, 2010 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 https://forums.phpfreaks.com/topic/196917-how-to-change-format-from-8005551212-to-800555-1212/ Share on other sites More sharing options...
andrewgauger Posted March 29, 2010 Share Posted March 29, 2010 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 https://forums.phpfreaks.com/topic/196917-how-to-change-format-from-8005551212-to-800555-1212/#findComment-1033847 Share on other sites More sharing options...
ghurty Posted March 30, 2010 Author Share Posted March 30, 2010 But how then do I take the results (case 10 for example) and assign it to a variable? Thanks Link to comment https://forums.phpfreaks.com/topic/196917-how-to-change-format-from-8005551212-to-800555-1212/#findComment-1033868 Share on other sites More sharing options...
Andy-H Posted March 30, 2010 Share Posted March 30, 2010 $formattedNum = formatCaller($cidnum); Link to comment https://forums.phpfreaks.com/topic/196917-how-to-change-format-from-8005551212-to-800555-1212/#findComment-1033890 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.