Jump to content

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


ghurty

Recommended Posts

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

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"

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

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.

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.