dongli7870 2015-01-13 17:47
浏览 54
已采纳

递归解析功能

I made a function that parses a certain page of a website(a forum thread). It should select users and their posts and then go on to the next page and do the same. While it does that, its return value is always null. I think I made a mistake with the recursion, but can't really figure it out.

Here is the function, I made it return only the userlist, for now.

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

And the call

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

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

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

Here, stuff is empty.

  • 写回答

1条回答 默认 最新

  • doushi3202 2015-01-13 17:57
    关注

    At the very least you would need to use the value returned from the recursive function:

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

    should be:

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

    Apart from that I'm not sure if you are returning the right information, probably (you'd need to check...) you need something like:

        //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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵