过滤掉外国号码或带有扩展名的号码不是一个好主意。这是我用于将常用数字转换为 CRM 注入的特定格式的示例。包括垃圾邮件评分。
<?php
function phoneNumber($value) {
$phoneScore = 0;
$numbers = preg_replace('![^0-9]!','',$value);
//if it's too small to be a US number, then ramp it up.
if (!empty($value)&&(strlen($numbers) < 10)){
$phoneScore = $phoneScore + 3;
} elseif (empty($value)){
$phoneScore = $phoneScore + 10;
}
return $phoneScore;
}
$numbers = array(
'(911 -535 -3535',
'800.555.1212',
'800 458 2180',
'800 - 458 2180',
'800 - 458-2180',
'',
'800-555-1212 ext# 212',
'+49 (0)69 974640',
'+86 (021) 5466-2808',
'+66 (02) 261-3525',
'+44 (020) 7626-0224',
'123456',
);
$patterns = array('![^0-9xX#+]!','!-+!','!-?[xX#]-?!','!#+!','!#!','!^-!');
$replacements = array("-","-",'#','#',' #','');
foreach ($numbers as $value){
$score = phoneNumber($value);
$clean_num = preg_replace($patterns,$replacements,$value);
echo "$value = Score $score.<br />\n Cleaned Number = $clean_num<br />\n<br />\n";
}
?>
输出如下所示:
(911 -535 -3535 = 0 分。清洁次数 = 911-535-3535
800.555.1212 = 0 分。清除次数 = 800-555-1212
800 458 2180 = 0 分。清洁次数 = 800-458-2180
800 - 458 2180 = 0 分。清洁次数 = 800-458-2180
800 - 458-2180 = 0 分。清洁次数 = 800-458-2180
= 得分 10。清洁次数 =
800-555-1212 分机# 212 = 得分 0。已清理号码 = 800-555-1212 #212
+49 (0)69 974640 = 0 分。清洁次数 = +49-0-69-974640
+86 (021) 5466-2808 = 0 分。清洁号码 = +86-021-5466-2808
+66 (02) 261-3525 = 0 分。清洁次数 = +66-02-261-3525
+44 (020) 7626-0224 = 0 分。清洁号码 = +44-020-7626-0224
123456 = 3 分。清洁次数 = 123456