Ed9c50a6db8b5e078b5ef84306a8477c

The equivalent of the non-standard DOM property 'innerHTML'.

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 !

543d0222ea3269453527c07141d48e4b

Rene Saarsoo

January 19, 2009, January 19, 2009 18:45, permalink

1 rating. Login to rate!

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.

0e9212587d373ca58e9bada0c15e6fe4

23434

May 18, 2009, May 18, 2009 14:05, permalink

No rating. Login to rate!

234234

Your refactoring





Format Copy from initial code

or Cancel