PHP - 在找到元素后从元素中获取链接

时间:2014-11-23 04:40:08

标签: php html xpath domdocument

我有以下代码......

<div class="outer">
<div>
<h1>Christmas</h1>
 <ul>
  <li>Holiday</li>
  <li>Fun</li>
  <li>Joy</li>
 </ul>
<h1>4th July</h1>
 <ul>
  <li>Fireworks</li>
  <li>Happy</li>
  <li>Spectral</li>
 </ul>
</div>
</div>
<div class="outer">
<div>
<h1>Christmas2</h1>
 <ul>
  <li>Holiday</li>
  <li>Fun</li>
  <li>Joy</li>
 </ul>
<h1>4th July</h1>
 <ul>
  <li>Fireworks2</li>
  <li>Happy</li>
  <li>Spectral</li>
 </ul>
</div>
</div>

我已经知道我可以找到DIV,然后在DIV中查看元素等... ...

$doc->loadHTML($output);    //$output being the text above
$xpath = new DOMXpath($doc);
$elements = $xpath->query('//div[@class="outer"]');  //Check outer

我知道以上3行将从列出的DIV中获取元素,但我真正希望能够获得[H1]的文本,然后显示每个H1旁边的[li]值..

我正在寻找的输出是......

Christmas - Holiday, Fun, Joy
4th July - Fireworks, Happy, Spectral
Christmas2 - Holiday, Fun, Joy
4th July2 - Fireworks, Happy, Spectral

2 个答案:

答案 0 :(得分:1)

是的,您可以继续使用xpath遍历标题上的元素并获取其后续兄弟列表。例如:

$doc = new DOMDocument();
$doc->loadHTML($output);
$xpath = new DOMXpath($doc);
$elements = $xpath->query('//div[@class="outer"]/div');
if($elements->length > 0) {
    foreach($elements as $div) {
        foreach ($xpath->query('./h1', $div) as $e) {
            $header = $e->nodeValue;
            $list = array();
            foreach ($xpath->query('./following-sibling::ul/li', $e) as $li) {

                $list[] = $li->nodeValue;
            }

            echo $header . ' - ' . implode(', ', $list) . '<br/>';
        }
        echo '<hr/>';
    }   
}

Sample Output

答案 1 :(得分:0)

我过去曾使用phpQuery来解决此类问题:

// include phpquery
require('phpQuery/phpQuery.php');
// initialize
$doc = phpQuery::newDocumentHTML($markup);
// get the text from the various elements
$h1Value = $doc['h1:first']->text(); // Christmas
// ... etc.

(未测试的)