【发布时间】:2020-10-24 12:24:38
【问题描述】:
我正在尝试查找文档中的所有 p 标签,如下所示:
$dom = new DOMDocument();
$html = '<p>First</p><p>Second</p><p>Third</p><h3>Test 2</h3><p>Fourth</p>';
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$itens = [];
$xp = new DOMXPath($dom);
$res = $xp->query('//p');
foreach ($res as $item) {
$itens[] = $item->nodeValue;
}
print_r($itens);
但是当 LIBXML_HTML_NOIMPLIED 开启时,它不能按预期工作。我明白了:
Array
(
[0] => FirstSecondThirdTest 2Fourth
[1] => Second
[2] => Third
[3] => Fourth
)
但我希望:
Array
(
[0] => First
[1] => Second
[2] => Third
[3] => Fourth
)
这里发生了什么?
【问题讨论】: