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: (800) 555-1212 to this format:8005551212. The variable that contains the number is the initial $to: function transfer($from,$to){ $this->log->debug("in function transfer($from, $to)"); if(empty($from) || empty($to)) { echo "Not sufficient parameters to create the call"; $this->log->debug("Not sufficient parameters to create the call"); return false; } $to = "sip:$to"; So for right now, $to ens up being "sip:(800)555-1212" I want it that it ends up being "sip:8005551212". Thanks Quote Link to comment Share on other sites More sharing options...
ignace Posted March 29, 2010 Share Posted March 29, 2010 $to = str_replace(array('(', ')', '-'), '', $to); Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted March 29, 2010 Share Posted March 29, 2010 function OnlyNum($str) { return preg_replace('/[^0-9]/', '', $str); } echo "sip:".OnlyNum($to); Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted March 29, 2010 Share Posted March 29, 2010 To clarify these items. ignace's code will trim out all "(" ")" and "-" of the number. My function will ONLY return numbers. SO if someone had "(800)@5#5!5$ - 12-12" for the phone number field ignace code would return "800@5#5!5$ 1212" and mine would return "8005551212" Quote Link to comment Share on other sites More sharing options...
ignace Posted March 29, 2010 Share Posted March 29, 2010 To clarify these items. ignace's code will trim out all "(" ")" and "-" of the number. My function will ONLY return numbers. SO if someone had "(800)@5#5!5$ - 12-12" for the phone number field ignace code would return "800@5#5!5$ 1212" and mine would return "8005551212" REGEX is IMO to much overhead for something this simple Quote Link to comment Share on other sites More sharing options...
ghurty Posted March 29, 2010 Author Share Posted March 29, 2010 Thank you for your help. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 29, 2010 Share Posted March 29, 2010 REGEX is IMO to much overhead for something this simple Well, that assumes this IS simple. True, if ALL the values are exactly in the same format, str_replace() would be faster. But, we are talking about hundredths if not thousandts of seconds. Nothing that would affect overall performance in the least. Plus, a regex expression will have more flexibility as you can create a single regex expression that could process many different types of formats - or the formats chould change and the regex would continue to work. With a str_replace() you would need to have specific logic to handle multiple formants and you would need to change it if the inputs were to ever change. Again, as long as the format will ALWAYS be the same, str_replace() will suffice. But, only if that is the case. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.