With XPath you can fetch '<style>...</style>' and '<style type="text/css">...</style>':
$oDomInput = new DOMDocument();
$oDomInput->loadHTML($sHtml);
$oDOMXPath = new DOMXPath($oDomInput);
$oDOMNodeList = $oDOMXPath->evaluate('//style');
$oDomOutput = new DOMDocument();
if ($oDOMNodeList->length > 0) {
for ($i = 0; $i < $oDOMNodeList->length; $i++) {
$oDomOutput->appendChild($oDomOutput->importNode($oDOMNodeList->item($i), true));
}
}
var_dump($oDomOutput->saveHTML());
However, this does not include style tags inside coditional comments '<!--[if gte IE 9]> ... <style> ... </style> <![endif]-->'.
With XPath you can fetch comments, conditional comments but also unwanted HTML comments:
$oDomInput = new DOMDocument();
$oDomInput->loadHTML($sHtml);
$oDOMXPath = new DOMXPath($oDomInput);
$oDOMNodeList = $oDOMXPath->evaluate('//comment()');
$oDomOutput = new DOMDocument();
if ($oDOMNodeList->length > 0) {
for ($i = 0; $i < $oDOMNodeList->length; $i++) {
$oDomOutput->appendChild($oDomOutput->importNode($oDOMNodeList->item($i), true));
}
}
var_dump($oDomOutput->saveHTML());
In order to remove HTML comments from the HTML string use this Regex:
$sHtml = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/', '', $sHtml);