这并不适用于所有格式,一些电话号码可以以不同的格式放置,不能仅通过检查 011 或长度来完成。您需要一份所有区号/组合的完整列表,以检查每个可能的长度。
http://support.hostgator.com/articles/specialized-help/making-international-calls-from-the-united-states
例如,您可能会看到英国 (UK) 的电话号码以其中一种方式表示:
(+44) (0)20 7930 4832 +44 20 7930 4832 020 7930 4832
简单的解决方案可能是。另外我对这段代码不满意,我确定是否可以简化大约一千次。
private function cleanNumber($uglyNumber){
$number = preg_replace("/[^0-9]/","",$uglyNumber);
if(($check = substr($number,0,3))!=='011'){
//NON 011 Numbers
switch(strlen($number)){
//Same Area Code
//123-4567
case 7:
$prefix = substr($number,0,3);
$SLN = substr($number,3,10);
$cleanNumber = $prefix.'-'.$SLN;
break;
//Domestic US
//No International Number
case 10:
$area = substr($number,0,3);
$prefix = substr($number,3,3);
$SLN = substr($number,6,10);
$cleanNumber = '('.$area.') ' .$prefix.'-'.$SLN;
break;
case 11:
// International Number [1]
$country = substr($number,0,1);
$area = substr($number,0,3);
$prefix = substr($number,4,3);
$SLN = substr($number,7,10);
$cleanNumber = '['.$country .'] ('.$area.') ' .$prefix.'-'.$SLN;
break;
case 12:
// International Number [12]
$country = substr($number,0,2);
$area = substr($number,0,3);
$prefix = substr($number,4,3);
$SLN = substr($number,7,10);
$cleanNumber = '['.$country .'] ('.$area.') ' .$prefix.'-'.$SLN;
break;
case 13:
// International Number [123]
$country = substr($number,0,3);
$area = substr($number,0,3);
$prefix = substr($number,4,3);
$SLN = substr($number,7,10);
$cleanNumber = '['.$country .'] ('.$area.') ' .$prefix.'-'.$SLN;
break;
default:
//dono
return $number;
}
}else{
switch(strlen($number)){
case 11:
// ExitUS 001 [1]
//No Area Code Country
$exitUs = substr($number,0,3);
$country = substr($number,3,1);
$prefix = substr($number,4,3);
$SLN = substr($number,7,11);
$cleanNumber = ''.$exitUs .' ['.$country.'] ' .$prefix.'-'.$SLN;
break;
case 12:
// ExitUS 001 [12]
//No Area Code Country
$exitUs = substr($number,0,3);
$country = substr($number,3,2);
$prefix = substr($number,4,3);
$SLN = substr($number,7,12);
$cleanNumber = ''.$exitUs .' ['.$country.'] ' .$prefix.'-'.$SLN;
break;
case 13:
// ExitUS 001 [123]
//No Area Code Country
$exitUs = substr($number,0,3);
$country = substr($number,3,3);
$prefix = substr($number,6,3);
$SLN = substr($number,9,13);
$cleanNumber = ''.$exitUs .' ['.$country.'] ' .$prefix.'-'.$SLN;
break;
case 14:
// ExitUS 001 [1]
// With Country Code
$exitUs = substr($number,0,3);
$country = substr($number,3,1);
$area = substr($number,4,3);
$prefix = substr($number,7,3);
$SLN = substr($number,10,14);
$cleanNumber = ''.$exitUs .' ['.$country.'] ('.$area.') ' .$prefix.'-'.$SLN;
break;
case 15:
// ExitUS 001 [12]
// With Country Code
$exitUs = substr($number,0,3);
$country = substr($number,3,2);
$area = substr($number,5,3);
$prefix = substr($number,8,3);
$SLN = substr($number,11,4);
$cleanNumber = ''.$exitUs .' ['.$country.'] ('.$area.') ' .$prefix.'-'.$SLN;
break;
case 16:
// ExitUS 001 [123]
// With Country Code
$exitUs = substr($number,0,3);
$country = substr($number,3,3);
$area = substr($number,6,3);
$prefix = substr($number,9,3);
$SLN = substr($number,12,4);
$cleanNumber = ''.$exitUs .' ['.$country.'] ('.$area.') ' .$prefix.'-'.$SLN;
break;
default:
//dono
return $number;
}
}
return $cleanNumber;
}