替换PHP中的span,但将内容保留在其中

问题描述 投票:1回答:2

我有以下字符串:

<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="">。我的字符串中不仅只有一个这样的块。

预先感谢您的帮助,也许对不起我的愚蠢问题-我仍在学习。

php html dom domdocument
2个回答
0
投票
DOMDocument

输出:

一些文字内容 span> span>

0
投票
然后是您的PHP:

<?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>

© www.soinside.com 2019 - 2024. All rights reserved.