innerHTML in php-dom
by kae verens on Jul.28, 2008, under php
DOM does not officially have an innerHTML parameter, but it’s incredibly useful. I found a need for something similar when working on some DOM stuff, so had to write a version.
function string_getInsertedString($long_string,$short_string,$is_html=false){
if($short_string>=strlen($long_string))return false;
$insertion_length=strlen($long_string)-strlen($short_string);
for($i=0;$i<strlen($short_string);++$i){
if($long_string[$i]!=$short_string[$i])break;
}
$inserted_string=substr($long_string,$i,$insertion_length);
if($is_html && $inserted_string[$insertion_length-1]=='<'){
$inserted_string='<'.substr($inserted_string,0,$insertion_length-1);
}
return $inserted_string;
}
function DOMElement_getOuterHTML($document,$element){
$html=$document->saveHTML();
$element->parentNode->removeChild($element);
$html2=$document->saveHTML();
return string_getInsertedString($html,$html2,true);
}
Okay, this is outerHTML, not inner, but if you want the innerHTML, then just do something like this:
$innerHTML=preg_replace('/^<[^>]*>(.*)<[^>]*>$/','\1',DOMElement_getOuterHTML($document,$element));
There is possibly a better way of doing this, but the above worked for me in the “throwaway” code I was writing.
5 Comments for this entry
2 Trackbacks / Pingbacks for this entry
-
Kae Verans’ Blog: innerHTML in php-dom | Development Blog With Code Updates : Developercast.com
July 28th, 2008 on 5:23 pm[...] Verans has written up a handy snippet of code to try to mimic the innerHTML property that Javascript lets you have access [...]
-
September 20th, 2008 on 2:46 pm
Hello!
This is a wonderful script, good work! It really saved my day.
Thank you so much! Hope its okey to use it as part in a little cms
system where building?
In any way, please send me a mail telling its okey.
Otherwise I will remove it.
Thanks!
Best regards
Adrian
September 20th, 2008 on 7:00 pm
Adrian – totally fine. you’re free to use it as you wish.
October 1st, 2008 on 11:13 am
Hi Guys,
There is an easy and clean way of doing it using DOMDocument class itself.
See the example below.
function convertNodeToDOM($domNode) {
//this function receives a node and converts it to Dom document
$doc = new DOMDocument();
$doc->appendChild($doc->importNode($domNode,true));
return $doc;
}
$tmpDOM = convertNodeToDOM($tmpDiv); //tmp div can be retrieves using getElementById etc
$innerHTML = saveHTML($tmpDOM);
Regards,
Lokesh
November 4th, 2008 on 4:44 pm
Expanding on Lokesh’s solution…
To get the innerHTML of a specific element ($elem_id) in a specific html file ($filepath), you have to accommodate multiple children in that element:
$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();
}
November 15th, 2008 on 11:53 am
HI,
Can i use innerhtml using Php?
Thanks
Om