Php 你是如何整理内联样式的
我正在尝试使用Tidy来清理和传输旧系统中的一些内容 系统有很多内联样式覆盖,我想完全删除它们(我不想将它们转换为类,只需删除它们) 我正在使用以下配置:Php 你是如何整理内联样式的,php,htmltidy,Php,Htmltidy,我正在尝试使用Tidy来清理和传输旧系统中的一些内容 系统有很多内联样式覆盖,我想完全删除它们(我不想将它们转换为类,只需删除它们) 我正在使用以下配置: $config = array( 'indent' => true, 'output-xhtml' => true, 'drop-font-tags' => true, 'clean' => true, 'merge-spans'=> true,
$config = array(
'indent' => true,
'output-xhtml' => true,
'drop-font-tags' => true,
'clean' => true,
'merge-spans'=> true,
'drop-proprietary-attributes'=> true,
);
然后像这样运行它:
$test = '<p><span style="font-size: 10px;">Some content goes here.</span></p>';
$tidy = new tidy;
$tidy->parseString($test, $config, 'utf8');
$body = $tidy->body();
var_dump($body->value);
$test='有些内容放在这里。;
$tidy=新的tidy;
$tidy->parseString($test,$config,'utf8');
$body=$tidy->body();
变量转储($body->value);
但产出仍然是:
<body>
<p>
<span style="font-size: 10px;">Some content goes here.</span>
</p>
</body>
这里有一些内容。
我怎样才能把style=“font-size:10px;”
部分也去掉,或者干脆把span
标签一起去掉呢
我在中看不到任何其他可以这样做的内容。您可以自己删除样式属性:
$test = '<p><span style="font-size: 10px;">Some content goes here.</span></p>';
$dom = new DOMDocument;
$dom->loadHTML($test);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//*[@style]'); // Find elements with a style attribute
foreach ($nodes as $node) {
$node->removeAttribute('style'); // Remove style attribute
}
$test = $dom->saveHTML();
$tidy = new tidy;
$tidy->parseString($test, $config, 'utf8');
$body = $tidy->body();
var_dump($body->value);
$test='有些内容放在这里。;
$dom=新的DOMDocument;
$dom->loadHTML($test);
$xpath=newdomxpath($dom);
$nodes=$xpath->query('/*[@style]');//查找具有样式属性的元素
foreach($node作为$node){
$node->removeAttribute('style');//删除样式属性
}
$test=$dom->saveHTML();
$tidy=新的tidy;
$tidy->parseString($test,$config,'utf8');
$body=$tidy->body();
变量转储($body->value);
Nice dave,我喜欢它。如果tidy不能删除不需要的属性,那么它似乎是tidy缺少的一个重要元素。