0

我试图在 sitepoint javascript 论坛中删除每个主题的第一篇文章。但是 DOM 解析器会为我提供站点点 JAVASCRIPT 论坛中每个主题的所有帖子。也许我没有正确遍历 DOM ?下面是我的代码:

<?php

class Sitepoint extends Controller
{
    public function index()
    {
        $this->load->helper('dom');
        header('Content-Type: text/html; charset=utf-8');
        echo '<ol>';

            $html = file_get_html('http://www.sitepoint.com/forums/javascript-15');

            foreach($html->find('a[id^="thread_title"]') as $topic) {
                $post =$topic->href;
                $posthtml = file_get_html($post);
                $posthtml->find('div[id^="post_message"]', 0);
                echo'<li>';
                echo $topic->plaintext.'<br>';
                echo $posthtml->plaintext.'<br>';
                echo'</li>';
            }
        echo '</ol>';
    }
}
4

1 回答 1

1

您忘记将结果分配给$posthtml->find变量:

foreach($html->find('a[id^="thread_title"]') as $topic) {
    $post =$topic->href;
    $posthtml = file_get_html($post);
    $posttext = $posthtml->find('div[id^="post_message_"]', 0);
    echo'<li>';
    echo $topic->plaintext.'<br>';
    echo $posttext->plaintext.'<br>';
    echo'</li>';
}
于 2011-03-10T15:02:50.317 回答