I am trying to edit html tags using DOMDocument :: loadHTML in php. The html data is part of html, not the whole page. I followed this page ( PHP - DOMDocument - should replace / replace an existing HTML tag with a new one ).
This should convert the pre-tags to div tags, but it gives a "Fatal error: Uncaught exception" DOMException with the message "Not Found Error". "
<?php $contents = <<<STR <pre>hi</pre> <pre>hello</pre> <pre>bye</pre> STR; $dom = new DOMDocument; @$dom->loadHTML($contents); foreach( $dom->getElementsByTagName("pre") as $nodePre ) { $nodeDiv = $dom->createElement("div", $nodePre->nodeValue); $dom->replaceChild($nodeDiv, $nodePre); } echo $dom->saveHTML(); ?>
[Edit] Although I am trying to iterate the node object backwards, I get this error: "Note: trying to get a non-object property ..."
<?php $contents = <<<STR <pre>hi</pre> <pre>hello</pre> <pre>bye</pre> STR; $dom = new DOMDocument; @$dom->loadHTML($contents); $domPre = $dom->getElementsByTagName('pre'); $length = $domPre->length; For ($i = $length; $i > -1 ; $i--) { $nodePre = $domPre->item($i); echo $nodePre->nodeValue . '<br />'; // $nodeDiv = $dom->createElement("div", $nodePre->nodeValue); // $dom->replaceChild($nodeDiv, $nodePre); } // echo $dom->saveHTML(); ?>
[Edit] Okey, decided. Since the response code has some error, I post the solution here. Thanks to everyone.
Decision:
<?php $contents = <<<STR <pre>hi</pre> <pre>hello</pre> <pre>bye</pre> STR; $dom = new DOMDocument; @$dom->loadHTML($contents); $domPre = $dom->getElementsByTagName('pre'); $length = $domPre->length; For ($i = $length - 1; $i > -1 ; $i--) { $nodePre = $domPre->item($i); $nodeDiv = $dom->createElement("div", $nodePre->nodeValue); $nodePre->parentNode->replaceChild($nodeDiv, $nodePre); } echo $dom->saveHTML(); ?>
Teno
source share