我是一个php新手,知道有一个简单的解决方案,但我似乎无法找到它。我希望返回用户输入的符号字段并应用格式。
static function getFormatedNum($num){
if(empty($num)){}else{
$first= "(".substr($num, 0, 3).")";
$second= substr($num,3,3)."-";
$third=substr($num,6,4);
$num=$first.$second.$third;}
return $num;
答案 0 :(得分:1)
假设输入格式为:5555555555
function getFormattedNum($num)
{
if($num){
return "(" . substr($num,0,3) . ")"
. substr($num,3,3) . "-"
. substr($num,6);
}
}
答案 1 :(得分:1)
这并不能解释所有格式,有些电话号码可以放在不同的格式中,而且不能通过检查011或长度来完成。您需要一个完整的区域代码/组合列表,以检查每个可能的长度。
http://support.hostgator.com/articles/specialized-help/making-international-calls-from-the-united-states 例如,您可能会看到英国(英国)的电话号码表达了以下方式之一:
(+ 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;
}
答案 2 :(得分:0)
更新:在同一个功能中完成所有工作
function getFormatedNum($num){
//first strip all the non-digit characters from the input
$num = preg_replace("/[^0-9]+/", "", $num);
//and only then re-format the phone-number
if(!empty($num)){
$first= "(".substr($num, 0, 3).")";
$second= substr($num,3,3)."-";
$third=substr($num,6,4);
$num=$first.$second.$third;
}
return $num;
}
//test
echo getFormatedNum("408.807.9565");
<强>输出:强>
(408)807-9565