DOM解析器:仅删除某些属性 [英] DOM parser: remove certain attributes only
问题描述
如何使用DOM解析器删除跨标签中的所有属性,但 这两个属性
How can I use DOM parser to remove all attributes in span tags but except these two attributes,
< span style =text-decoration:underline;> cultura< / span>
接受
< span style =text-decoration:line-through> heart< / span>
接受
拒绝这个,
< span style =font-family:lang =EN-US> May< / span>
接受
可以吗?
我的其他工作代码 post 我做了,
My working code from the other post I made,
$content = '
<span style="text-decoration: underline;">cultura</span>l <span style="text-decoration: line-through;">heart</span>
<span style="font-family: " lang="EN-US">May</span>
';
$dom = new DOMDocument();
$dom->loadHTML($content);
foreach( $dom->getElementsByTagName( "span" ) as $span )
{
foreach( $span->attributes as $attrib )
{
$span->removeAttributeNode( $attrib );
}
}
$content = $dom->saveHTML();
但是这个代码将删除 span
标签...
But this code will remove all attributes inside the span
tags...
推荐答案
您需要手动进行。
DOM处理HTML属性,而不是CSS属性。
DOM handles HTML attributes, not CSS properties.
您需要访问样式属性, explode
它的值使用;
作为分隔符,然后循环查找要取消设置的值的数组。
You need to access the style attribute, explode
it's value using ;
as a delimiter, then loop the array looking for the value you want to unset.
这篇关于DOM解析器:仅删除某些属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!