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.