如果javascript被禁用,并且您希望在PHP或Regex中使用(XXX)XXX-XXXX,您如何过滤(和替换)电话


How do you filter (and replace) phone# input if javascript disabled and you want (XXX)XXX-XXXX exactly, in PHP or Regex

我有一个jQuery,它使用掩码处理得很好,问题是当用户没有javascript时。这是罕见的,但我们希望有一个回落。现在,我们的正则表达式回退是模块中的一个内置工具,可以确认10个数字,仅此而已。我希望将XXX-XXX-XXXX的输入、XXXXXXXXXX的输入或其他任何输入转换为(XXX)XXX-XXXX。我愿意用PHP编写代码来处理这个问题,但如果有一个正则表达式或一些我还没有见过的标准表达式,我会感兴趣。这个想法是找到一些完全证明的东西,这样就可以将其传递到数据库中。此表单的示例位于http://www.gestationaldiabetic.com.

我不是一个PHP爱好者,但最简单的事情就是去掉除数字之外的所有内容,然后在传递到DB之前重新创建字符串。

只要你有10位数字,就让用户随意输入。

Welp,我使用以下正则表达式:

^[^2-9]*([2-9])'D*('d)'D*('d)[^2-9]*([2-9])(?:'D*('d)[^02-9]*([02-9])|[^02-9]*([02-9])'D*('d))'D*('d)'D*('d)'D*('d)'D*('d).*$

然后使用替换模式:

$1$2$3-$4$5$6$7$8-$9$10$11$12

它允许其中任何一个被重新格式化,如所示:

Input                                                                Output
---------------------                                                --------
2345678905                                                           234-567-8905
2345678905                                                           234-567-8905
234-569-8956                                                         234-569-8956
223.5876954                                                          223-587-6954
4444444444                                                           444-444-4444
 2359996574                                                          235-999-6574
 777-873 6542                                                        777-873-6542
      (800) 874 8321                                                 800-874-8321
223...587...9999                                                     223-587-9999
1-800-345-6734                                                       800-345-6734
8.5.3-2.9.7-6.5.4.3                                                  853-297-6543
+1 (976) 566-7763                                                    976-566-7763
   0154388640         .           -              33                  543-886-4033
%4*555@123$5^78                                                      455-512-3578
#445#2984$32@9                                                       445-298-4329
$123,456,987.20                                                      234-569-8720
 123,456,987.20                                                      234-569-8720
 $23,456,987.20$                                                     234-569-8720
3/9/1954 @21:33                                                      391-954-2133
12345678901234567890                                                 234-567-8901
55555555555555555555555555                                           555-555-5555
lkajshdf28jksa23:''''fg[]ewr]?.,,ewf3412233445566gwqerq              282-334-1223
$123,456,987.20 dollars                                              234-569-8720
$223,456,987.20                                                      223-456-9872
222-211-45674                                                        222-214-5674

重要注意事项:它解析前十个可能代表电话号码的符合数字,有效地不允许输入不符合维基百科上NANP文章中规定的规则的电话号码。

表达式中间的五个组(组4-8)是"交换码"的第一个数字(#4),然后是交换码的最后两个数字(分别为#5/#7和#6/#8),如果它们不是1

基本上,任何真实有效的电话号码都应该被正确地解析和重新格式化。

其他一些选项包括:

This allows getting and reformatting an extension (indicated by `x`, `ext.`, `extension`, plus a few variations of those and only if all extension digits are contiguous), if supplied:
Parsing Expression
------------------
(?i)^'D*1?'D*([2-9])'D*('d)'D*('d)'D*('d)'D*('d)'D*('d)'D*('d)'D*('d)'D*('d)'D*('d)[^x]*?'s*(?:(?:e?(x)(?:'.|t'.?|tension)?)'D*('d+))?.*$
Replacement
-----------
$1$2$3-$4$5$6-$7$8$9$10 $11$12

This does not enforce ALL of the NANP rules, but does most of them and is bug free, according to my tests:
Parsing Expression
------------------
^[^2-9]*([2-9])'D*('d)'D*('d)'D*([2-9])'D*('d)'D*('d)'D*('d)'D*('d)'D*('d)'D*('d).*$
Replacement
-----------
$1$2$3-$4$5$6-$7$8$9$10

如果你愿意,我可以进一步解释或给你另一个版本!

此外,对于"编译"代码的效率也有一些需要说明的地方(JavaScript通常不是真正编译的,.Net和Java不是完全编译的,许多语言只是实时解析的…),但对于紧凑的源代码也有很多需要说明的方面-你决定是让一个表达式(比如regex)为你做所有的工作,还是创建所有的循环、检查,解析等。

这是一个伪的。

function format_phone($num) {
    $num = preg_replace('/[^'d]/','',$num);
    if (strlen($num) != 10) return 0;
    // this requires the number to contain 10 digits
    return preg_replace('/^('d{3})('d{3})('d{4})$/','($1)$2-$3',$num);
}
$res = format_phone('0123456789');
if (!$res) {
    echo 'Phone number should contain a 3-digit area code and a 7-digit local phone number.';
} else {
    echo $res;
    // INSERT the number in your DB
}