循环函数中的PHP数组

循环函数中的PHP数组,php,regex,arrays,function,loops,Php,Regex,Arrays,Function,Loops,我有php函数从某个页面获取项目,该项目有分页 function get_content($link){ $string = file_get_contents($link); $regex = '/https?\:\/\/[^\" ]+/i'; preg_match_all($regex, $string, $matches); //this is important foreach($matches as $final){ $newa

我有php函数从某个页面获取项目,该项目有分页

function get_content($link){
    $string = file_get_contents($link);
    $regex = '/https?\:\/\/[^\" ]+/i';
    preg_match_all($regex, $string, $matches);

    //this is important
    foreach($matches as $final){
        $newarray[] = $final;
    }

    if(strpos($string,'Next Page')){ //asumme the pagination is http://someserver.com/content.php?page=2
        get_content($link);
    }

    return $newarray;
} 
问题:

  • 在这种情况下是否可以使用循环功能

  • 当我尝试它时,为什么我只得到1页数组?我的意思是,如果有5个页面,每个页面有50个链接,我在打印结果时只能得到50个链接,而不是250个


  • 谢谢

    您从未将递归值设置到正在构建的主数组中。而且您根本不需要更改
    $link
    来更改从中获取数据的文件

    您需要执行以下操作:

    if(strpos($result,'Next Page')){ //asumme the pagination is http://someserver.com/content.php?page=2
        $sub_array = get_content($link . '?page=x'); // you need some pagination identifier probably
        $newarray = array_merge($new_array, $sub_array);
    }
    

    为什么要使用正则表达式而不是
    $\u GET
    ?很抱歉,伙计,我忘了,我创建的函数是从页面中提取链接,页面有分页。假设页面有5个页面,所以我只获取了5次内容,并将所有URL保存在“$newarray”数组中。谢谢,我找到了我只需要数组合并函数的答案@Mike:yea,我从原始页面获取的分页,因为它们使用一些“键”来标识页面ex:if(strpos($result,'Next page'))这只是一个示例过滤器,用于知道下一页是否可用,如果可用,它将处理另一个代码以获取“下一个url”。感谢Mike和Machavity的快速响应。非常感谢。