note 86803 modified in class.domelement by danbrown
| From: | danbrown@php.net | Date: | Tue, 04 Nov 2008 16:17:08 +0000 |
| Subject: | note 86803 modified in class.domelement by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146124@lists.php.net to get a copy of this message | ||
Although it may be preferable to use the dom to manipulate elements, sometimes it's useful to
actually get the innerHTML from a document element (e.g. to load into a client-side editor).
To get the innerHTML of a specific element ($elem_id) in a specific html file ($filepath):
<?php
$innerHTML = '';
$doc = new DOMDocument();
$doc->loadHTMLFile($filepath);
$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();
}
?>
--was--
Although it may be preferable to use the dom to manipulate elements, sometimes it's useful to
actually get the innerHTML from a document element (e.g. to load into a client-side editor).
To get the innerHTML of a specific element ($elem_id) in a specific html file ($filepath):
$innerHTML = '';
$doc = new DOMDocument();
$doc->loadHTMLFile($filepath);
$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();
}
http://php.net/manual/en/class.domelement.php