Skip to content

Instantly share code, notes, and snippets.

@broskees
Forked from komlenic/DOMElement.innerHTML.php
Last active September 27, 2021 06:27
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 broskees/13eb8fac184d56975133f0c2da341849 to your computer and use it in GitHub Desktop.
Save broskees/13eb8fac184d56975133f0c2da341849 to your computer and use it in GitHub Desktop.
Strip Outer Tags of an HTML string in PHP
<?php
// See http://www.php.net/manual/en/class.domelement.php#101243
function stripOuterTags($html) {
$doc = new \DOMDocument();
$doc->loadHTML($html);
$element = $doc->documentElement;
$newhtml = '';
foreach($element->childNodes as $node) {
$newhtml .= $element->ownerDocument->saveHTML($node);
}
return $newhtml;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment