用于匹配和提取北美 phone 号码的正则表达式
Regex to match and extract North American phone numbers
我需要从文本中匹配并提取 phone 个数字...phone 个格式为以下格式的数字:
589-845-2889
(589)-845-2889
589.845.2889
589 845 2889
5898452889
(589) 845 2889
下面的操作与上面的操作非常吻合:
preg_match_all("/(\()?\d{3}(?(1)\))[-. ]?\d{3}[-. ]?\d{4}/", $test, $result);
所以我需要扩展它以支持国际前缀,例如1
或+1
。因此,它还应该匹配 1(589) 845 2889
、+1(589) 845 2889
、1589 845 2889
、1 589 845 2889
、15898452889
等
如有任何帮助,我们将不胜感激。
对于示例数据,您可以使用:
^(?:\+?1\h*)?(?:(\()?\d{3}(?(1)\))[-. ]?\d{3}[-. ]?\d{4})$
模式匹配:
^
字符串开头
(?:\+?1\h?)?
可选地匹配 1 可选地以 +
为前缀或后跟 space
(\()?\d{3}(?(1)\))
括号内是否匹配3位
[-. ]?
匹配可选 -
.
\d{3}
匹配 3 个数字
[-. ]?
匹配可选 -
.
\d{4}
匹配4位数字
$
字符串结尾
例子
$re = '/^(?:\+?1\h?)?(\()?\d{3}(?(1)\))([-. ]?)\d{3}\d{4}$/m';
$str = '589-845-2889
(589)-845-2889
589.845.2889
589 845 2889
5898452889
(589) 845 2889
1(589) 845 2889
+1(589) 845 2889
1589 845 2889
1 589 845 2889
15898452889
+1207 244 7002
+1 207 244 7002
(589) 845-2889
+1(589) 845-2889
1(589) 845-2889';
preg_match_all($re, $str, $matches);
print_r($matches[0]);
输出
Array
(
[0] => 589-845-2889
[1] => (589)-845-2889
[2] => 589.845.2889
[3] => 589 845 2889
[4] => 5898452889
[5] => (589) 845 2889
[6] => 1(589) 845 2889
[7] => +1(589) 845 2889
[8] => 1589 845 2889
[9] => 1 589 845 2889
[10] => 15898452889
[11] => +1207 244 7002
[12] => +1 207 244 7002
)
替代方法:
您可以将所有不同的类型转换为 1 种由 .
分隔的通用语法。
现在,您可以尝试进行 2 次检查。
- 如果都是数字,长度为10或11
- 或者如果它遵循
+1.207.333.4444
或 207.333.4444
或 +1(207).333.4444
或 (207).333.4444
的格式。
片段:
<?php
function isValid($str){
$str = preg_replace('/[.\-\s]/','.',$str);
return preg_match('/^\d{10,11}$/', $str) === 1 ||
preg_match('/^((\+?\d)?\.?(\d{3}|\(\d{3}\)))\.(\d{3})\.(\d{4})$/', $str) === 1;
}
建议:
不是在 UI 上给出一个简单的文本框,而是给出 3-4 个由一些文本分隔的块,比如 -
或 .
。这样,在后端处理它们或添加 javascript 验证变得容易。
.input_ph{
width:35px;
}
<html>
<body>
<span>+</span><input type="textbox" class="input_ph" />
<span>-</span>
<span>(</span><input type="textbox" class="input_ph" />
<span>)</span>
<span>-</span> <input type="textbox" class="input_ph" />
<span>-</span> <input type="textbox" class="input_ph" />
</body>
</html>
我需要从文本中匹配并提取 phone 个数字...phone 个格式为以下格式的数字:
589-845-2889
(589)-845-2889
589.845.2889
589 845 2889
5898452889
(589) 845 2889
下面的操作与上面的操作非常吻合:
preg_match_all("/(\()?\d{3}(?(1)\))[-. ]?\d{3}[-. ]?\d{4}/", $test, $result);
所以我需要扩展它以支持国际前缀,例如1
或+1
。因此,它还应该匹配 1(589) 845 2889
、+1(589) 845 2889
、1589 845 2889
、1 589 845 2889
、15898452889
等
如有任何帮助,我们将不胜感激。
对于示例数据,您可以使用:
^(?:\+?1\h*)?(?:(\()?\d{3}(?(1)\))[-. ]?\d{3}[-. ]?\d{4})$
模式匹配:
^
字符串开头(?:\+?1\h?)?
可选地匹配 1 可选地以+
为前缀或后跟 space(\()?\d{3}(?(1)\))
括号内是否匹配3位[-. ]?
匹配可选-
.
\d{3}
匹配 3 个数字[-. ]?
匹配可选-
.
\d{4}
匹配4位数字$
字符串结尾
例子
$re = '/^(?:\+?1\h?)?(\()?\d{3}(?(1)\))([-. ]?)\d{3}\d{4}$/m';
$str = '589-845-2889
(589)-845-2889
589.845.2889
589 845 2889
5898452889
(589) 845 2889
1(589) 845 2889
+1(589) 845 2889
1589 845 2889
1 589 845 2889
15898452889
+1207 244 7002
+1 207 244 7002
(589) 845-2889
+1(589) 845-2889
1(589) 845-2889';
preg_match_all($re, $str, $matches);
print_r($matches[0]);
输出
Array
(
[0] => 589-845-2889
[1] => (589)-845-2889
[2] => 589.845.2889
[3] => 589 845 2889
[4] => 5898452889
[5] => (589) 845 2889
[6] => 1(589) 845 2889
[7] => +1(589) 845 2889
[8] => 1589 845 2889
[9] => 1 589 845 2889
[10] => 15898452889
[11] => +1207 244 7002
[12] => +1 207 244 7002
)
替代方法:
您可以将所有不同的类型转换为 1 种由 .
分隔的通用语法。
现在,您可以尝试进行 2 次检查。
- 如果都是数字,长度为10或11
- 或者如果它遵循
+1.207.333.4444
或207.333.4444
或+1(207).333.4444
或(207).333.4444
的格式。
片段:
<?php
function isValid($str){
$str = preg_replace('/[.\-\s]/','.',$str);
return preg_match('/^\d{10,11}$/', $str) === 1 ||
preg_match('/^((\+?\d)?\.?(\d{3}|\(\d{3}\)))\.(\d{3})\.(\d{4})$/', $str) === 1;
}
建议:
不是在 UI 上给出一个简单的文本框,而是给出 3-4 个由一些文本分隔的块,比如 -
或 .
。这样,在后端处理它们或添加 javascript 验证变得容易。
.input_ph{
width:35px;
}
<html>
<body>
<span>+</span><input type="textbox" class="input_ph" />
<span>-</span>
<span>(</span><input type="textbox" class="input_ph" />
<span>)</span>
<span>-</span> <input type="textbox" class="input_ph" />
<span>-</span> <input type="textbox" class="input_ph" />
</body>
</html>