我一直绞尽脑汁想如何格式化一个简单的手机号码但却无法思考如何做到这一点。一位朋友告诉我,我可以使用preg_replace函数,但我不确定,因为我已经读过它但不熟悉它。基本上我想要做的是通过一个字符串说一条消息,其中包含:“你好,我的名字是约翰,你可以打电话给我07771234567谢谢”并替换它,所以它写着“你好,我的名字是约翰,你可以打电话给我07xx12x4xx7谢谢“。换句话说,用“x”替换部分数字。
我也知道有些人可能会写这样的手机:“0777 123 4567”因此需要它来剥离任何空白区域。此外,它需要查找数字的开头和结尾,尽管我使用了一个名为stripos()的函数来查找数字的开头,方法是查找“07”的起始位置。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
这样的东西应该有效,尽管可能有直接的正则表达式方法:
$foo = '07771234567';
$foo = preg_replace( '/\s+/', ' ', $foo ); // remove whitespaces
$foo = str_split($foo); // split into an array so we can loop over it more easily
$replace_with_x = array(2, 3, 6, 8, 9); // the positions of numbers you want to replace
// loop through your phone number, and replace with x where necessary
for($i=0; $i<len($foo); $i++) {
if(in_array($i, $replace_with_x)) {
$foo[$i] = 'x';
}
}
答案 1 :(得分:0)
http://www.braemoor.co.uk/software/telnumbers.shtml
您可以从此网站下载PHP功能,以便验证所有不同类型的英国电话号码。
我正在使用这是一个实时系统atm所以它可以解决。
祝你好运
答案 2 :(得分:0)
这并不考虑所有格式,有些电话号码可以放在不同的格式中,不能通过检查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;
}