|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44762 DOMElement need a innerHTML
Submitted: 2008-04-17 14:40 UTC Modified: 2019-09-22 21:49 UTC
Avg. Score:4.4 ± 1.1
Reproduced:15 of 16 (93.8%)
Same Version:9 (60.0%)
Same OS:5 (33.3%)
From: xwisdom at yahoo dot com Assigned:
Status: Wont fix Package: DOM XML related
PHP Version: * OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-04-17 14:40 UTC] xwisdom at yahoo dot com
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 :)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-04 16:16 UTC] 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();
    $innerHTML .= $tmp_doc->saveHTML();
 [2009-06-22 18:36 UTC] 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>
 [2009-06-23 02:20 UTC] 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 to see if they can assist you.
 [2011-04-08 18:15 UTC]
-Package: Feature/Change Request +Package: DOM XML related
 [2015-01-09 01:12 UTC]
-Operating System: Windows XP +Operating System: * -PHP Version: 5.2.5 +PHP Version: *
 [2019-09-22 21:49 UTC]
-Status: Open +Status: Wont fix
 [2019-09-22 21:49 UTC]
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 which will most likely go into PHP 8 there is a simple three liner liner that allows this:

    $fragment = $dom->createDocumentFragment(); 
    echo $dom->saveXML($fragment);

This will modify the XML doc though and remove the child nodes from $element.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 15:01:32 2024 UTC