递归解析功能

时间:2015-01-13 17:47:48

标签: php html parsing recursion

我创建了一个解析网站某个页面(论坛帖子)的函数。它应该选择用户和他们的帖子,然后继续下一页并做同样的事情。虽然它这样做,但它的返回值始终为null。我认为我在递归方面犯了一个错误,但无法弄明白。

这是函数,我现在只返回用户列表。

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];

这里的东西是空的。

1 个答案:

答案 0 :(得分:1)

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

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;
}