Phone Number RegExp - PHP

Matching a phone number, with a regular expression, and disregarding any extraneous characters.

Useful for searches, not validation.


<?php
$aPhoneNumbers=array(
'7085556232',
'1(708)555-6232',
'(708)555-6232',
'708.555.6232',
'1.708.555.6232',
'1.708.555.6232 ext. 123',
'17085556232',
'7215556232',
'1(721)555-6232',
'(721)555-6232',
'721.555.6232',
'1.721.555.6232',
'17215556232'
);

$sPhone='7085556232';
$rPattern='/(\d)/';
$rReplace='\D*${1}';
$sRegExp='/'.preg_replace($rPattern,$rReplace,$sPhone).'/';

echo 'Regular Expression: '.$sRegExp."\n";

foreach ($aPhoneNumbers as $k => $v)
        check($sRegExp,$v);


function check($sRegExp,$sPhone)
{
echo '$sPhone: '.$sPhone.' ? '.preg_match($sRegExp,$sPhone)."\n";
}
?>

Output

Regular Expression: /\D*7\D*0\D*8\D*5\D*5\D*5\D*6\D*2\D*3\D*2/
$sPhone: 7085556232 ? 1
$sPhone: 1(708)555-6232 ? 1
$sPhone: (708)555-6232 ? 1
$sPhone: 708.555.6232 ? 1
$sPhone: 1.708.555.6232 ? 1
$sPhone: 1.708.555.6232 ext. 123 ? 1
$sPhone: 17085556232 ? 1
$sPhone: 7215556232 ? 0
$sPhone: 1(721)555-6232 ? 0
$sPhone: (721)555-6232 ? 0
$sPhone: 721.555.6232 ? 0
$sPhone: 1.721.555.6232 ? 0
$sPhone: 17215556232 ? 0