|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-04-17 14:40 UTC] xwisdom at yahoo dot com
Description: ------------ I would like to request that a innerHTML property (or method) be added to DOMElement class. This would make it so much easier to retrieve and set the html or xml content of a node. Right now we have to use fragments with several lines of to get the job done. It would be so much better to be able to do it in one line of code :) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 01:00:01 2025 UTC |
In the meantime, here's a utility function that you may find helpful: $innerHTML = ''; $elem = $doc->getElementById($elem_id); // loop through all childNodes, getting html $children = $elem->childNodes; foreach ($children as $child) { $tmp_doc = new DOMDocument(); $tmp_doc->appendChild($tmp_doc->importNode($child,true)); $innerHTML .= $tmp_doc->saveHTML(); }The innerHTML property is not part of the spec and will not be added. There is a simpler code snippet than the one in the comments before using saveXML with a node argument: $innerHtml = implode("\n", array_map(function ($node) { return $node->ownerDocument->saveXML($node); }, iterator_to_array($d->documentElement->childNodes)); With https://github.com/php/php-src/pull/4709 which will most likely go into PHP 8 there is a simple three liner liner that allows this: $fragment = $dom->createDocumentFragment(); $fragment->append(...$element->childNodes); echo $dom->saveXML($fragment); This will modify the XML doc though and remove the child nodes from $element.