php.net |  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
Votes:21
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: xwisdom at yahoo dot com
New email:
PHP Version: OS:

 

 [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 :)






Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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();
    $tmp_doc->appendChild($tmp_doc->importNode($child,true));		
    $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 http://raxanpdi.com to see if they can assist you.
 [2011-04-08 18:15 UTC] jani@php.net
-Package: Feature/Change Request +Package: DOM XML related
 [2015-01-09 01:12 UTC] ajf@php.net
-Operating System: Windows XP +Operating System: * -PHP Version: 5.2.5 +PHP Version: *
 [2019-09-22 21:49 UTC] beberlei@php.net
-Status: Open +Status: Wont fix
 [2019-09-22 21:49 UTC] beberlei@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 19:01:28 2024 UTC