0

考虑以下 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>
4

1 回答 1

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

为了防止错误弹出

于 2013-03-05T12:57:19.330 回答