php - 正则表达式:显示所有电话号码

标签 php regex

我想查看字符串中的所有电话号码。现在它在数组“匹配”中只有一个数字我怎样才能得到我数组中的所有数字?

$str = "djvdsfhis 0647382938 rdfrgfdg tel:0647382938 rfgdfgfd 06 47 38 29 
38 fdgdfrggfd tel:06-47382938 cxgvfdgsfdc";
$arr = '~\d{2}-\d{8}|\d{10}~';
$success = preg_match($arr, $str, $match);
if ($success) {
    echo "Match: ".$match[0]."<br />"; 
    print_r($match);
}

我得到这个作为输出:

djvdsfhis ffgfg 0647382938 rdfrgfdg tel:0647382938 rfgdfgfd 06 47 38 29 38 fdgdfrggfd tel:06-47382938 cxgvfdgsfdc

Match: 0647382938
Array ( [0] => 0647382938 )

但我想要这样的数组:

Array ( [0] => 0647382938 [1] => 0647382938 [2] => 06-47382938

最佳答案

你应该使用 preg_match_all .这将输出一个包含正则表达式所有结果的数组,在本例中是一个数字数组。

$str = "djvdsfhis 0647382938 rdfrgfdg tel:0647382938 rfgdfgfd 06 47 38 29 
38 fdgdfrggfd tel:06-47382938 cxgvfdgsfdc";
$arr = '~\d{2}-\d{8}|\d{10}~';
$success = preg_match_all($arr, $str, $match);
if ($success) {
    print_r($match);
}

在这里测试:

http://sandbox.onlinephpfunctions.com/code/350d10b1be46ce3a5851d7671750bac28f9110f0

关于php - 正则表达式:显示所有电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54402941/

相关文章:

javascript - 用于检查字符串的正则表达式,该字符串必须包含空格并且是字母数字

javascript - VSCode 搜索替换正则表达式

c# - 正则表达式在线匹配,但在 C# 代码中不匹配

javascript - 如何计算javascript中字符串中的空行

php - MySQL中根据id连接两个表

php - 在 SQL SELECT 语句和 PDO 中使用大于或等于 (>=) 和小于或等于 (<=)

php - MySQL根据第二个表字段值批量更新表

php - 使用 mysql_real_escape_string 时更多的斜线

php - Yii 间接修改重载属性

javascript - 使用撇号作为千位分隔符设置数字格式