Req #44762 [Opn->Wfx]: DOMElement need a innerHTML
| From: | beberlei@php.net | Date: | Sun, 22 Sep 2019 21:49:00 +0000 |
| Subject: | Req #44762 [Opn->Wfx]: DOMElement need a innerHTML | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222870@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=44762&edit=1
ID: 44762
Updated by: beberlei@php.net
Reported by: xwisdom at yahoo dot com
Summary: DOMElement need a innerHTML
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: DOM XML related
Operating System: *
PHP Version: *
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2009-06-23 02:20:59] xwisdom at yahoo dot com
I really hope the PHP Developers will see the need for this feature and add it in.
You can try using this the DOM features found over at http://raxanpdi.com to see if they can assist you.
------------------------------------------------------------------------
[2009-06-22 18:36:15] roly426 at gmail dot com
I was trying to use the above method to get the innerHTML in an HTML document but sine these
functions are designed for XML, some HTML will get changed. Example, <br /> will get changed
to <br>
------------------------------------------------------------------------
[2008-11-04 16:16:17] patrick at patricksmith dot org
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();
}
------------------------------------------------------------------------
[2008-04-17 14:40:05] 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 :)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=44762&edit=1