[英]regex pattern in php to find phone number between plain text
I am trying to get the phone number between the plain text. 我正在尝试获取纯文本之间的电话号码。 But here, I am facing some typical problem.
但是在这里,我面临一些典型的问题。 The pattern which I have written is not matching all types of phone numbers.
我写的模式不匹配所有类型的电话号码。
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);
which is giving me the first phone number , but not the second one. 这是给我的第一个电话号码,而不是第二个。 Plz help me in getting that pattern also.
Plz也帮助我获得了这种模式。
Output:
Array
(
[0] => Array
(
[0] => (216) 499 1001
)
----
---
Thanks in advance! 提前致谢!
Hi I will write the algorithm for this 嗨,我将为此编写算法
1) Fetch the whole string into an array 2) start scan document using for loop(till end of line) 3) use is_numeric function finding finding numbers and count total digits then use an if statement (10 digits= phone number, 5 digits=postal code like that) 4) if it is a phone number move the value in to a temp array else next line(use space as separator) 4)now u can process all 1)将整个字符串提取到数组中2)使用for循环(到行尾)开始扫描文档3)使用is_numeric函数查找数字并计算总位数,然后使用if语句(10位=电话号码,5位=像这样的邮政编码)4)如果它是电话号码,则将值移入临时数组,否则下一行(使用空格作为分隔符)4)现在,您可以处理所有
if you want to filter phone number with area code or service provider, u can use following codes 如果要使用区号或服务提供商过滤电话号码,则可以使用以下代码
<?php
$strings = array('+91', '+11', '+97');
foreach ($strings as $testcase) {
if (ctype_digit($testcase)) {
echo "Match found .\n";
} else {
echo "Not matching.\n";
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.