Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created March 26, 2013 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpfiddle/5249347 to your computer and use it in GitHub Desktop.
Save phpfiddle/5249347 to your computer and use it in GitHub Desktop.
PHP DOM Sample.
<?php
function innerHTML($el) {
$doc = new DOMDocument();
$doc->appendChild($doc->importNode($el, TRUE));
$html = trim($doc->saveHTML());
$tag = $el->nodeName;
return preg_replace('@^<' . $tag . '[^>]*>|</' . $tag . '>$@', '', $html);
}
$doc = new DOMDocument();
// A corrupt HTML string
$doc->loadHTML('<HTML><A HREF="ss">asd</A>');
$body = $doc->getElementsByTagName('body')->item(0);
print htmlspecialchars(innerHTML($body));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment