1 2 3 4 5 6 7 8
<?php function innerHTML($node){ $doc = new DOMDocument(); foreach ($node->childNodes as $child) $doc->appendChild($doc->importNode($child, true)); return $doc->saveHTML(); }
Refactorings
No refactoring yet !
Rene Saarsoo
January 19, 2009, January 19, 2009 18:45, permalink
This code is pretty short and simple. But to understand it you need to know what importNode() does. It's not that obvious when you aren't that familiar with DOM API. At least for me it was quite hard to *get* it.
There doesn't seem to be any better way to do it - as far as I digged in PHP manual. It should also be efficient enough when you aren't working with really big documents - but then you probably shouldn't use DOM anyway.
What this code really needs is a comment. A comment that explains *why* it does things the way it does.
The equivalent of the non-standard DOM property 'innerHTML'.