views:

58

answers:

4

How do i change any of the following to just numbers

1-(999)-999-9999 
1-999-999-9999
1-999-9999999
1(999)-999-9999
1(999)999-9999

i want the final product to be 19999999999

+2  A: 

The easiest way would be to strip everything out of your string that's not a number and then see if you end up with a 10 digit number (or 11 if you're making the 1 mandatory):

$string = "1-(999)-999-9999";
$number = preg_replace('/[^0-9]/', "", $string); // results in 19999999999
if (strlen($number) == 11)
{
  // Probably have a phone number
}
Daniel Vandersluis
15 digits for international
Chad
Yeah, but I'm going on the formats he's provided.
Daniel Vandersluis
+2  A: 

try

preg_replace('\D', '', $string);

This will filter out any non digits.

CEich
A: 

You dont really need regex for this...

$number = str_replace(array('-', '(', ')'), '', $number);
OIS
what about things like `x`, `ext`, `.`, `+`, etc
Chad
If you know the input will be like one of the examples then thats all you need. If you dont know the input then you need more validation anyway.
OIS
A: 
preg_replace('[\D*]', '', '1-(999)-999-9999');
Russ