JavaScript-like innerHTML access in PHP

As part of an update to the Five Filters Full-Text RSS service, I’ve been porting some JavaScript code (Arc90’s current version of Readability) to PHP. It contains a lot of DOM manipulation which translates very easily – thanks to PHP5’s DOM support. But one thing I wasn’t able to do was manipulate the DOM tree through the innerHTML property.

In JavaScript, it’s very easy to do. The Mozilla Developer Network’s page on innerHTML gives the following example:

var content = element.innerHTML;  
// Returns a string containing the HTML syntax describing all 
// of the element's descendants
element.innerHTML = content;  
// Removes all of element's descendants, parses the content 
// string and assigns  the resulting nodes as descendants of 
// the element.

Using PHP’s magic getter and setter methods, it’s possible to extend DOMElement to achieve this type of access and manipulation. My attempt at doing it is JSLikeHTMLElement. Here’s an example of how to use it (with relevant lines highlighted):

require_once 'JSLikeHTMLElement.php';
$doc = new DOMDocument();
$doc->registerNodeClass('DOMElement', 'JSLikeHTMLElement');
$doc->loadHTML('<div><p>Para 1</p><p>Para 2</p></div>');
$elem = $doc->getElementsByTagName('div')->item(0);

// print innerHTML
echo $elem->innerHTML; // prints '<p>Para 1</p><p>Para 2</p>'

// set innerHTML
$elem->innerHTML = 'FF';

// print document (with our changes)
echo $doc->saveXML();

Download: JSLikeHTMLElement.php. Feedback appreciated.

This entry was posted in Code and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

12 Comments


  1. Fatal error: Uncaught Error: Call to undefined function ereg() in /home/keyvan70/public_html/keyvan.net/wp-content/themes/thematic/library/extensions/comments-extensions.php:175 Stack trace: #0 /home/keyvan70/public_html/keyvan.net/wp-content/themes/thematic/library/extensions/discussion.php(10): thematic_commenter_link() #1 /home/keyvan70/public_html/keyvan.net/wp-includes/class-walker-comment.php(174): thematic_comments(Object(WP_Comment), Array, 1) #2 /home/keyvan70/public_html/keyvan.net/wp-includes/class-wp-walker.php(145): Walker_Comment->start_el('', Object(WP_Comment), 1, Array) #3 /home/keyvan70/public_html/keyvan.net/wp-includes/class-walker-comment.php(135): Walker->display_element(Object(WP_Comment), Array, 1, 0, Array, '') #4 /home/keyvan70/public_html/keyvan.net/wp-includes/class-wp-walker.php(321): Walker_Comment->display_element(Object(WP_Comment), Array, 1, 0, Array, '') #5 /home/keyvan70/public_html/keyvan.net/wp-includes/comment-template.php(2097): Walker->paged_walk(Array, -1, 0, 0, Array) #6 /home/key in /home/keyvan70/public_html/keyvan.net/wp-content/themes/thematic/library/extensions/comments-extensions.php on line 175