简单的 html dom 解析器获取元素之间的 html

问题描述 投票:0回答:0

我正在使用 PHP Simple HTML Dom 库从网页获取 HTML。我需要在“div.page-content”内的第一个标签和第一个“h4”标签之间获取 HTML。例子:

<div class="page-content">
   First text
   <p>Second text</p>
   <div>Third text</div>
   <p>More text</p>
   <h4>Subtitle 1</h4>
   <p>bla bla</p>
   <p>bla bla</p>
   <h4>Subtitle 2</h4>
   <p>bla bla</p>
   <p>bla bla</p>
</div>

我试过这样做:

$start = $html->find('div.page-content p', 0);
while ( $next = $start->next_sibling() ) {
    if ( $next->tag == 'h4')
        break;
    else{
        echo $next->plaintext;
        echo '<br/>';
        
        $start = $next;
    }
}

但它只获取第二个、第三个和更多的文本,没有第一个文本。

我需要全部获取:

 First text
 <p>Second text</p>
 <div>Third text</div>
 <p>More text</p>
php html dom
© www.soinside.com 2019 - 2024. All rights reserved.