我正试图在纯文本之间得到电话号码。但在这里,我面临着一些典型的问题。我所写的模式并不是匹配所有类型的电话号码。Ex:
$string = 'sample text sample text sample text (216) 499 1001 sample text sample text (262) 335 60 76-77 sample text sample text sample text';
$string = str_replace('\n', PHP_EOL, $string);
//my regex pattern
$regex = '/(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?\s+?/';
preg_match_all($regex, $string, $matches);
print_r($matches);这是给我的第一个电话号码,但不是第二个。请也帮我弄到那个图案。
Output:
Array
(
[0] => Array
(
[0] => (216) 499 1001
)
----
---提前感谢!
发布于 2014-03-24 19:42:44
嗨,我会为这个写算法
( 1)将整个字符串提取到数组中2)使用for循环开始扫描文档(直到行尾) 3)使用is_numeric函数查找数字并计数总数字,然后使用if语句(10 digits=电话号码,5 digits=postal代码) 4)如果是电话号码,将值移动到下一行的临时数组中(以空格作为分隔符)4)现在你可以处理所有
如果您想使用地区代码或服务提供商过滤电话号码,您可以使用以下代码
<?php
$strings = array('+91', '+11', '+97');
foreach ($strings as $testcase) {
if (ctype_digit($testcase)) {
echo "Match found .\n";
} else {
echo "Not matching.\n";
}
}
?>https://stackoverflow.com/questions/22609850
复制相似问题