使用PHP DOM replaceChild将节点替换为文本节点

时间:2017-10-13 16:26:11

标签: php dom

我走了一个DOMDocument,当我到达某些标签时,我需要保留其内容但删除标签。为此,我尝试用文本节点替换原始节点。

$child = $section->childNodes->item($i);
switch ($child->tagName) {
  case 'dontNeedIt':
    $child->parentNode->replaceChild(new DOMText($child->textContent), $child);

之后我希望$ child成为一个文本节点,但没有变化。

1 个答案:

答案 0 :(得分:0)

请尝试一下,我只是采用了一个字符串值,在其中放置了一些锚标记,并从其Node值替换了每个锚标记。

<?php

$contents = 'Lorem ipsum dolor sit amet, metus <a href="first.html">First Anchor</a> curabitur orci feugiat aliquam, lorem aenean nobis quisque, pellentesque odio ut, augue duis nisl aliquam tortor inceptos, sed habitasse mauris. Enim eu suspendisse elementum, diam congue aliqua in accumsan eu nunc, cras quam nam magnis. Pellentesque aliquet tristique neque eu faucibus <a href="second.html">Second Anchor</a>, duis nulla. Cursus aliquet faucibus ut, felis sociosqu rhoncus magnis faucibus, urna at nunc euismod mauris. Tortor condimentum natoque metus neque sint in, eu convallis egestas id nulla quisque cupidatat, duis aptent sed nec iaculis praesent elementum. Volutpat velit libero id ridiculus, ut posuere nec. Parturient donec suspendisse, <a>Lorry Ipsum</a> nisl aliquam, venenatis elementum sed suscipit fermentum in, est blandit sagittis luctus ipsum at. Nisl consectetuer molestie ullamcorper senectus velit turpis, <a href="https://stackoverflow.com/questions/37964378/domelement-replace-html-value">Stack overflow</a> suspendisse erat neque wisi, arcu libero, fringilla vitae pede, turpis molestiae pellentesque. <a href="http://php.net/manual/en/domnode.appendchild.php">Contact Us</a>';

$dom = new DOMDocument;
@$dom->loadHTML($contents);

echo $contents;

$elements = $dom->getElementsByTagName("a");
for ($i = $elements->length - 1; $i >= 0; $i --) {
    $nodePre = $elements->item($i);
    //$nodeDiv = $dom->createElement("span", $nodePre->nodeValue);
    //$nodePre->parentNode->replaceChild($nodeDiv, $nodePre); way to replace with new node
    $nodePre->parentNode->replaceChild(new DOMText($nodePre->nodeValue), $nodePre); // way to replace with node value
}
echo $dom->saveXML(); // $dom->saveHTML() according to requirement

?>

输出:

  <?xml version="1.0" standalone="yes"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>Lorem ipsum dolor sit amet, metus First Anchor curabitur orci feugiat aliquam, lorem aenean nobis quisque, pellentesque odio ut, augue duis nisl aliquam tortor inceptos, sed habitasse mauris. Enim eu suspendisse elementum, diam congue aliqua in accumsan eu nunc, cras quam nam magnis. Pellentesque aliquet tristique neque eu faucibus Second Anchor, duis nulla. Cursus aliquet faucibus ut, felis sociosqu rhoncus magnis faucibus, urna at nunc euismod mauris. Tortor condimentum natoque metus neque sint in, eu convallis egestas id nulla quisque cupidatat, duis aptent sed nec iaculis praesent elementum. Volutpat velit libero id ridiculus, ut posuere nec. Parturient donec suspendisse, Lorry Ipsum nisl aliquam, venenatis elementum sed suscipit fermentum in, est blandit sagittis luctus ipsum at. Nisl consectetuer molestie ullamcorper senectus velit turpis, Stack overflow suspendisse erat neque wisi, arcu libero, fringilla vitae pede, turpis molestiae pellentesque. Contact Us</p></body></html>