我正试图获得纯文本之间的电话号码。但在这里,我面临着一些典型的问题。我所写的模式并不匹配所有类型的电话号码。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
)
----
---
提前感谢!
嗨,我将为这个编写算法
1)获取整个字符串到一个数组中2)使用for循环开始扫描文档(直到行尾)3)使用is_numeric函数查找查找数字并计数总数字,然后使用if语句(10位数字=电话号码,5位数字=邮政编码)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";
}
}
?>