Created
January 25, 2024 16:35
-
-
Save derweili/7956fff0c050f61d9d5182176dba65f0 to your computer and use it in GitHub Desktop.
removeStylesFromHtmlMarkup.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeStylesFromHtmlMarkup($markup) { | |
function removeElementsByTagName($tagName, $document) { | |
$nodeList = $document->getElementsByTagName($tagName); | |
for ($nodeIdx = $nodeList->length; --$nodeIdx >= 0; ) { | |
$node = $nodeList->item($nodeIdx); | |
$node->parentNode->removeChild($node); | |
} | |
} | |
// create a new DomDocument object | |
$doc = new DOMDocument(); | |
// load the HTML into the DomDocument object (this would be your source HTML) | |
$doc->loadHTML($markup); | |
removeElementsByTagName('style', $doc); | |
return str_replace(['<body>', '</body>'], '', $doc->saveHTML($doc->getElementsByTagName('body')->item(0))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment