【发布时间】:2020-01-31 05:09:47
【问题描述】:
我需要从以下字符串创建一个数组。
$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('./following-sibling::p', $tag);
$question_answer[] = [
'question' => $tag->nodeValue,
'answer' => $next_element->item(0)->nodeValue,
];
}
echo '<pre>';
print_r($question_answer);
echo '</pre>';
结合@Kevin 的建议,该建议非常适合 p 标签并产生以下输出:
Array
(
[0] => Array
(
[question] => Heading one
[answer] => Lorem ipsum dolor
)
[1] => Array
(
[question] => Heading two
[answer] =>
)
[2] => Array
(
[question] => Heading three
[answer] =>
)
)
现在我只需要解决answer 下一个标签何时是无序列表或表格。对于表格,我只对 td 标签感兴趣。
【问题讨论】:
标签: php html dom domdocument