递归解析函数

Recursive parsing function

我做了一个函数来解析网站的某个页面(论坛线程)。它应该 select 用户和他们的帖子,然后转到下一页并执行相同的操作。在这样做的同时,它的 return 值始终为空。我想我在递归时犯了一个错误,但我真的想不通。

这是函数,我暂时只将它设为 return 用户列表。

function getWinners( $thread,$userlist,$postlist ) {
    //libxml_use_internal_errors(true);
    $html = file_get_html( $thread );


    //get users
    $users=$html->find( 'li[class="postbitlegacy postbitim postcontainer old"] div[class=username_container] strong span' );
    foreach ( $users as $user )
        //echo $user . '<br>';
        array_push( $userlist, $user );
    //get posts
    $posts=$html->find( 'li[class="postbitlegacy postbitim postcontainer old"] div[class=postbody] div[class=content]' );
    foreach ( $posts as $post )
        // echo $post . '<br>';
        array_push( $postlist, $post );
    //check if there is a next page
    if ( $next=$html->find( 'span[class=prev_next] a[rel="next"]', 0 ) ) {
        $testa='http://forums.heroesofnewerth.com/'.$next->href;
        // echo $testa. '<br>';
        $html->clear();
        unset( $html );

        //recursive calls until the last page of the forum thread
        getWinners( $testa,$userlist,$postlist );

     //no more thread, return users
    }else return $userlist;
}

和通话

$thread='http://forums.heroesofnewerth.com/showthread.php?553261';

    $userlist=array();
    $postlist=array();

 $stuff=getWinners( $thread,$userlist,$postlist);
 echo $stuff[0];

这里,东西是空的。

至少你需要使用递归函数返回的值:

getWinners( $testa,$userlist,$postlist );

应该是:

return getWinners( $testa,$userlist,$postlist );
// or, more likely:
return array_merge($users, getWinners($testa,$userlist,$postlist));

除此之外,我不确定您是否返回了正确的信息,可能(您需要检查...)您需要类似的东西:

    //cursive calls until the last page of the forum thread
    return array_merge($userlist, getWinners($testa,$userlist,$postlist));
}
else {
    //no more thread, return users
    return $userlist;
}