Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从另一个数组中提取正则表达式输出数组_Php_Regex - Fatal编程技术网

Php 从另一个数组中提取正则表达式输出数组

Php 从另一个数组中提取正则表达式输出数组,php,regex,Php,Regex,所以我试图从文本文件中提取电话号码,文本文件的每一行代表一个用户配置文件。到目前为止,我的代码打开文本文件,通过数组分隔每一行,然后使用正则表达式查找电话号码。我的问题是如何打破正则表达式结果,以便将每个数字存储到一个变量中?到目前为止,我的代码只显示了我需要的第二个数组,比如第二个foreach,因为每行用户数据可以包含1到6个电话号码 <?php $array = explode("\n", file_get_contents('data\samd

所以我试图从文本文件中提取电话号码,文本文件的每一行代表一个用户配置文件。到目前为止,我的代码打开文本文件,通过数组分隔每一行,然后使用正则表达式查找电话号码。我的问题是如何打破正则表达式结果,以便将每个数字存储到一个变量中?到目前为止,我的代码只显示了我需要的第二个数组,比如第二个foreach,因为每行用户数据可以包含1到6个电话号码

 <?php
        $array = explode("\n", file_get_contents('data\samdata_000030.txt'));
        foreach ($array as $string){
        echo $string."<br><br><br>";
        preg_match_all('/\b[0-9]{3}\s*[-]?\s*[0-9]{3}\s*[-]?\s*[0-9]{4}\b/',$string,$matches);
        echo '<pre>';
        print_r($matches[0]);
        echo '</pre>';
    }

添加第二个foreach并正确显示数据的解决方案


只需将第二个
foreach
嵌套到外部。你的最终目标是什么?。那么,如果在一行中找到6个电话号码,你想创建6个变量吗?是的,这是正确的,当我嵌套另一个foreach时,我只会得到一堆错误。让我试试,然后把代码贴上去,这样你就可以看到了。我希望解决了这个问题。:)
foreach($matches[0] as $matched) {
        echo $matched;
}