Php DOM和Xpath - 替换节点但保留旧节点的子节点

时间:2013-03-05 12:36:14

标签: php dom xpath

考虑以下html:

<html>
    <title>Xyz</title>
    <body>
        <div>
            <div class='mycls'>
                <div>1 Books</div>
                <div>2 Papers</div>
                <div>3 Pencils</div>
            </div>
        </div>
    <body>
</html>
$dom = new DOMDocument();
$dom->loadHTML([loaded html of remote url through curl]);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('html/body/div[@class="mycls"]');

直到这里工作正常,我需要替换节点以获得以下内容:

<body>
        <div>
            <span>
                <div>1 Books</div>
                <div>2 Papers</div>
                <div>3 Pencils</div>
            </span>
        </div>
    <body>

1 个答案:

答案 0 :(得分:1)

以下内容适用于您:

$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$oldNode = $xpath->query('//div[@class="mycls"]')->item(0);
$span = $dom->createElement('span');

if ($oldNode->hasChildNodes()) {
    $children = [];
    foreach ($oldNode->childNodes as $child) {
        $children[] = $child;
    }
    foreach ($children as $child) {
        $span->appendChild($child->parentNode->removeChild($child));
    }
}

$oldNode->parentNode->replaceChild($span, $oldNode);

echo htmlspecialchars($dom->saveHTML());

演示:http://codepad.viper-7.com/WNTrR5

请注意,在演示中我还修复了完全破坏的HTML: - )

如果您的演示实际上是您从cURL调用中返回的HTML,并且您无法更改它(无法控制它),您可以这样做:

$libxmlErrors = libxml_use_internal_errors(true); // at the start

libxml_use_internal_errors($libxmlErrors); // at the end

防止错误弹出