我正在尝试通过收集分发的<style>
标记并将其全部删除然后将新的标记添加到head
来安排文档,我已编写此代码段,但遗漏了如何添加新的style
标记到标题
ob_start('ob_postprocess');
// ob_postprocess is our custom post processing function
function ob_postprocess($buffer){
$d = new DOMDocument('1.0','UTF-8');
$d->loadHTML($buffer);
$x = new DOMXPath($d);
$t = $x->evaluate("//text()");
$cc = array();
foreach($d->getElementsByTagName('style') as $style){
$cc[]= $style->nodeValue;
$style->parentNode->removeChild($style);
}
foreach($d->getElementsByTagName('head') as $head){
//add new style tag
}
$buffer = $d->saveHTML($d);
return $buffer;
}