Skip to content

Instantly share code, notes, and snippets.

@derweili
Created January 25, 2024 16:35
Show Gist options
  • Save derweili/7956fff0c050f61d9d5182176dba65f0 to your computer and use it in GitHub Desktop.
Save derweili/7956fff0c050f61d9d5182176dba65f0 to your computer and use it in GitHub Desktop.
removeStylesFromHtmlMarkup.php
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