编程语言
首页 > 编程语言> > PHP DOM解析器移动关闭Div标签

PHP DOM解析器移动关闭Div标签

作者:互联网

这是我的代码:

$myHtml = '
<div class="div-class">
    <p>text</p>

    <p><a href="#">text</a></p>
</div>

<ul class="some-class">
    <li><a href="#" target="_blank" title="something something"><img src="" alt=""></a>
    </li>
    <li><a href="" target="_blank" title=""><img src="" alt=""></a>
    </li>
    <li><a href="" target="_blank" title=""><img src=""></a>
    </li>
</ul>
';

$doc = new \DOMDocument();
$doc->loadHTML($myHtml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new \DOMXPath($doc);
$anchors = $xpath->query("//a[@title='something something']");
$list = $xpath->query("//ul[@class='some-class']")[0];
foreach ($anchors as $a) {
    $list->removeChild($a->parentNode);
}

var_dump($doc->saveHTML());

本质上,我试图删除一个列表项,其中包含标题为“某事物”的锚标记.但是,当我在应用更改后保存html时,列表会在div标记内移动.为什么会这样?谢谢.

解决方法:

loadHTML()尝试纠正语法,并且它不喜欢ul元素是无父对象的,所以它将它移到div中.如果将它全部包裹在body标签周围,它将正常工作.

loadHTML()实际上应该自动为你做包装,但是你设置了LIBXML_HTML_NOIMPLIED标志,这会禁用它.

<?php
$myHtml = '
<html>
<body>
<div class="div-class">
    <p>text</p>

    <p><a href="#">text</a></p>
</div>

<ul class="some-class">
    <li><a href="#" target="_blank" title="something something"><img src="" alt=""></a>
    </li>
    <li><a href="" target="_blank" title=""><img src="" alt=""></a>
    </li>
    <li><a href="" target="_blank" title=""><img src=""></a>
    </li>
</ul>
</body>
</html>
';

$doc = new \DOMDocument();
$doc->loadHTML($myHtml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new \DOMXPath($doc);
$anchors = $xpath->query("//a[@title='something something']");
$list = $xpath->query("//ul[@class='some-class']")[0];
foreach ($anchors as $a) {
    $list->removeChild($a->parentNode);
}

var_dump($doc->saveHTML());

Demo

或者,没有LIBXML_HTML_NOIMPLIED标志:

<?php
$myHtml = '
<div class="div-class">
    <p>text</p>

    <p><a href="#">text</a></p>
</div>

<ul class="some-class">
    <li><a href="#" target="_blank" title="something something"><img src="" alt=""></a>
    </li>
    <li><a href="" target="_blank" title=""><img src="" alt=""></a>
    </li>
    <li><a href="" target="_blank" title=""><img src=""></a>
    </li>
</ul>
';

$doc = new \DOMDocument();
$doc->loadHTML($myHtml, LIBXML_HTML_NODEFDTD);
var_dump (libxml_get_errors());
$xpath = new \DOMXPath($doc);
$anchors = $xpath->query("//a[@title='something something']");
$list = $xpath->query("//ul[@class='some-class']")[0];
foreach ($anchors as $a) {
    $list->removeChild($a->parentNode);
}

var_dump($doc->saveHTML());

Demo

标签:php,string,html,domparser
来源: https://codeday.me/bug/20190627/1304944.html