解析h2和PHP中的下一个标记

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

我需要根据以下字符串创建一个数组。

$body = '<h2>Heading one</h2>
         <p>Lorem ipsum dolor</p>

         <h2>Heading two</h2>
         <ul>
           <li>list item one.</li>
           <li>List item two.</li>
         </ul>

         <h2>Heading three</h2>
         <table class="table">
           <tbody>
             <tr>
               <td>Table data one</td>
               <td>Description of table data one</td>
             </tr>
             <tr>
               <td>Table data two</td>
               <td>Description of table data two</td>
             </tr>
           </tbody>
         </table>';

我可以将h2标记用作获取'question'值的第一个索引。

$dom = new \DOMDocument();
$dom->loadHTML($body);
$xPath = new \DOMXpath($dom);

$question_answer = [];
$tags = $dom->getElementsByTagName('h2');
foreach ($tags as $tag) {
  $next_element = $xPath->query("//p/following-sibling::*[1]", $tag)->item(1)->nodeValue;
  $question_answer[] = [
    'question' => $tag->nodeValue,
    'answer' => $next_element,
  ];
}

echo '<pre>';
print_r($question_answer);
echo '</pre>';

此代码

 $next_element = $xPath->query("//p/following-sibling::*[1]", $tag)->item(1)->nodeValue;

不起作用。哪些查询将Lorem ipsum dolor作为answer / $next_element?哪种查询对列表项和表内容有效?

php dom
1个回答
0
投票

由于要迭代每个h2标签,所以请相对于当前标签使用following-sibling::p

foreach ($tags as $tag) {
    $next_element = $xPath->query('./following-sibling::p', $tag);
    if ($next_element->length <= 0) continue; //skip it if p not found
    $question_answer[] = [
        'question' => $tag->nodeValue,
        'answer' => $next_element->item(0)->nodeValue,
    ];
}
© www.soinside.com 2019 - 2024. All rights reserved.