我有以下字符串:
<span style="font-size: 13px;">
<span style="">
<span style="">
<span style="font-family: Roboto, sans-serif;">
<span style="">
Some text content
</span>
</span>
</span>
</span>
</span>
并且我想使用PHP将此字符串更改为以下字符串:
<span style="font-size: 13px;">
<span style="font-family: Roboto, sans-serif;">
Some text content
</span>
</span>
我没有任何想法,该怎么做,因为当我尝试使用str_replace替换<span style="">
时,我不知道如何替换</span>
并将内容保留在里面。我的下一个问题是,我不确切知道我的琴弦中有多少<span style="">
。我的字符串中不仅只有一个这样的块。
预先感谢您的帮助,也许对不起我的愚蠢问题-我仍在学习。
DOMDocument
输出:
一些文字内容 span> span><?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<!-- remove spans with empty styles -->
<xsl:template match="*[@style and string-length(./@style) = 0]">
<xsl:apply-templates />
</xsl:template>
<!-- catch all to copy any elements that aren't matched in other templates -->
<xsl:template match="*">
<xsl:copy select=".">
<!-- copy the attributes of the element -->
<xsl:copy-of select="@*" />
<!-- continue applying templates to this element's children -->
<xsl:apply-templates select="*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>