1

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

Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – JYelton Aug 10 '10 at 15:10

4 Answers4

2

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
  • 91,582
  • 23
  • 169
  • 153
2

try

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

This will filter out any non digits.

CEich
  • 31,956
  • 1
  • 16
  • 15
0

You dont really need regex for this...

$number = str_replace(array('-', '(', ')'), '', $number);
OIS
  • 9,833
  • 3
  • 32
  • 41
  • what about things like `x`, `ext`, `.`, `+`, etc – CaffGeek Aug 10 '10 at 15:19
  • 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 Aug 10 '10 at 15:23
0
preg_replace('[\D*]', '', '1-(999)-999-9999');
Russ
  • 133
  • 5