【问题标题】:Removing styling from HTML从 HTML 中删除样式
【发布时间】:2011-08-20 03:53:08
【问题描述】:

我有一个充满产品描述的数据库,其中充斥着可怕的计算机生成的 HTML,并充斥着不同的样式信息……样式属性、字体标签、背景属性……

我必须重新设计网站,但首先我需要从产品描述中删除所有样式。在有人建议手动操作之前,有 100,000 种产品。我认为 PHP 中的一些创造性正则表达式可能会起到作用。

理想情况下,我想删除所有 HTML 并只保留纯文本,但描述包含表格和表格的表格......所以这只会以眼泪结束。

期待您的创意解决方案:)

编辑-

再想一想,我也可以在 VBA 中执行此操作,因为我可以将它们导出到 Excel 工作表。所以 PHP 或 VBA 解决方案会很棒。

编辑-

    <div class="XXXX-template-06">
          <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="694" id="AutoNumber1">
            <tbody><tr>
              <td width="516" height="18" bgcolor="#999966" align="center">
              <p align="center"><font face="Verdana" color="#FFFFFF"><b>Mont Blanc Scott Roof mounted cycle bike carrier<br>
              <br>
              Part Number: 728540</b></font></p></td>
              <td width="178" height="18" bgcolor="#999966" align="center">
              <a href="/shippingcalculator.html?SKU=728540" target="_blank"><img border="0" src="http://images.ZZZZpro.com/2145/" width="88" height="33"></a></td>
            </tr>
            <tr>
              <td width="694" height="57" bgcolor="#CCCC99" align="center" colspan="2">
              <b><font face="Verdana" size="2" class="CustomStyle-CycleCarrier">
    <script type="text/javascript">
    <!--function click() { if (event.button==2) { alert('All graphics, descriptions and other information, including the HTML code of this listing are the property of XXXX Limited and may not be reproduced in any form without the express permission of XXXX Limited. Email us: sales@XXXX.com'); } } document.onmousedown=click // -->
    <!---->
    <!---->
    <!---->
    <!---->
    <!---->
    <!---->
    <!---->
    <!---->
    <!---->
    <!---->
    <!---->
    <!----> -->
    </script>


    <div align="center">
      <center>
        <table height="336" background="http://images.ZZZZpro.com/2145/I/21/fade1.jpg" width="680" border="0">
          <tbody><tr>
            <td height="49" width="136"><p align="center"><img height="62" src="http://XXXXbiz.ipage.com/XXXX/Images/Mont%20Blanc/montblanc.jpg" width="165" border="0"></p></td>
            <td height="49" width="378"><p align="center"><font face="Verdana" color="#0000ff" size="5"><u><strong>Mont Blanc </strong></u></font><u><strong><font face="Verdana" color="#0000FF" size="5">Scott Roof Bar Rack 1 Cycle Carrier</font></strong></u></p></td>
            <td height="49" width="146"><img height="69" src="http://images.ZZZZpro.com/2145/I/20/logomed.gif" width="174" border="0"></td>
          </tr>
          <tr>
            <td height="241" colspan="3" width="672"><hr><p align="center"><img height="223" src="http://XXXXbiz.ipage.com/XXXX/Images/Mont%20Blanc/scottlrg.jpg" width="237" border="0"></p><p><font color="black"><b>Scott</b> </font></p><ul><li>Stylish, easy to use roof mounted cycle carrier, distinctive oval carrying bar.<br></li><li>Extra Soft Frame clamps hold cycle safely and gently<br></li><li>Extra wide wheel holders take the fattest tyres<br></li><li>Strong Webbing straps fasten wheels securely to carrier<br></li><li><font size="3" color="black">Upright, roof bar mounted, locking cycle carrier<br></font></li><li><font size="3" color="black">&nbsp;Locks to roof rails and locks bikes<br></font></li><li><font size="3" color="black">&nbsp;Quick and easy to use<br></font></li><li><font size="3" color="black">Adjustable for most cycle styles</font></li></ul><center><table cellspacing="0" width="100%" cellpadding="20" border="0" height="1" class="featuretable">
                  <tbody><tr>
                    <td height="55" class="featuretd" width="110"><p align="center"><a target="_blank" href="http://www.montblancuk.co.uk/support/inst/scott.pdf"><img width="20" alt="Open document" src="http://espimages.biz/2145/I/20/mount_link.gif" border="0" height="20"></a></p></td>
                    <td height="55" class="featuretd">To view Fitting Instructions in PDF format please click the spanner</td>
                  </tr>
                </tbody></table>
                <table height="317">
                  <tbody><tr class="technicaltr" valign="top">
                    <td height="1" class="technicalfirstcolumn"><font class="technicalheader">Technical data</font></td>
                    <td height="1" class="technicalsecondcolumn"><p><font class="heading1">Mont </font>Blanc Scott</p><p align="center"><img height="107" src="http://XXXXbiz.ipage.com/XXXX/Images/Mont%20Blanc/scottfaint.jpg" width="127" border="0"></p></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>Max number of bikes</div></td>
                    <td height="21" class="technicalsecondcolumn"><div>1</div></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="18" class="technicalfirstcolumn"><div>Load capacity (kg)</div></td>
                    <td height="18" class="technicalsecondcolumn"><div>15 KG</div></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>Weight (kg)</div></td>
                    <td height="21" class="technicalsecondcolumn"><div>2.2KG</div></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>Fits frame-dimensions (mm)</div></td>
                    <td height="21" class="technicalsecondcolumn">Up to 80mm</td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>Fits wheel-dimensions</div></td>
                    <td height="21" class="technicalsecondcolumn"><div>All</div></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>Locks bikes to carrier</div></td>
                    <td height="21" class="technicalsecondcolumn"><div>Yes</div></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>Locks carrier to car</div></td>
                    <td height="21" class="technicalsecondcolumn"><div>Yes</div></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>Tilt function, with bikes</div></td>
                    <td height="21" class="technicalsecondcolumn"><div>NA</div></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>TÜV/EuroBE approved</div></td>
                    <td height="21" class="technicalsecondcolumn"><div>NA</div></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>Fullfills City Crash norms</div></td>
                    <td height="21" class="technicalsecondcolumn"><div>NA</div></td>
                  </tr>
                  <tr class="technicaltr" valign="top">
                    <td height="21" class="technicalfirstcolumn"><div>Miscellaneous</div></td>
                    <td height="21" class="technicalsecondcolumn"><div><p>Fits all types of Roof Bars,</p></div></td>
                  </tr>
                </tbody></table>
                <p align="center">
                  <font size="2" face="Verdana">The cycle carrier is 
                  guaranteed for Five year from date of purchase.                  
<br>                  
<br>We stock a wide range of towbars and towing accessories.                   
<a href="mailto:sales@XXXX.com?subject=Witter ZX88 Cycle Carrier"><br>Click 
                  here to email us</a> if you require details of our other 
                  towing equipment.</font>
                </p>


<hr>                
              </center>

            </td>

          </tr>
        </tbody></table>
      </center>

    </div>

  <br>
              Please note that with the Type of cycle carrier where you mount it
              <br>
              onto a flange ball you may need the long reach ball which will <br>
              allow you enough clearance from the bumper</font></b></td>
            </tr>
            <tr>
              <td width="694" height="57" bgcolor="#CCCC99" align="center" colspan="2">
              <a href="http://www.XXXXeuro.ZZZZprostorefront.co.uk/products/728540-mont-blanc-scott-roof-mounted-cycle-bike-carrier-728540.html" target="_blank"><img border="0" src="http://images.ZZZZpro.com/2145/" width="55" height="40"></a>
              <b><font face="Verdana" size="2">Not from the UK ? Click the flag
              to purchase this item from our EU site </font></b><a href="http://www.XXXXeuro.ZZZZprostorefront.co.uk/products/728540-mont-blanc-scott-roof-mounted-cycle-bike-carrier-728540.html" target="_blank"><img border="0" src="http://images.ZZZZpro.com/2145/" width="57" height="40"></a></td>
            </tr>
          </tbody></table>
</div>

编辑-

通过它,我认为我需要摆脱以下内容:

属性: 风格 彩色 背景

标签: 字体

【问题讨论】:

  • 如果您提供示例产品说明,您可能会得到更好的答案。
  • 向问题添加示例
  • 那是一些非常糟糕的 HTML。祝你好运!
  • 我之前用过HTML Tidy清理HTML,运气很好(虽然我在PHP下没用过)。不过,显然有 PHP 绑定 (see this page)。
  • 放弃右键禁用器,想偷东西的人会知道为什么,禁用右键不会比正常情况下保护您的财产,事实上它可能只会激怒试图访问基本功能的合法访问者.

标签: php html css regex vba


【解决方案1】:

我建议使用 XSLT 去除所有不需要的内容。一个简单的身份模板将是一个很好的起点。

【讨论】:

  • 但是如果 HTML 被随意复制/粘贴或者格式错误,XSLT 可能无法很好地应对。在这种情况下,我会采用可怕的正则表达式方法。
  • 这似乎是正确的方法。将 HTML 解析为 DOM,应用适当的 XSLT,然后序列化回 HTML。这将提供比简单地剥离标签更好的控制。例如,&lt;font face="Verdana" color="#FFFFFF"&gt;&lt;b&gt; 标记模式可以转换为&lt;span class="part-description"&gt;,并且可以在不丢失信息的情况下删除样式。
  • 使用 HTML Tidy 输出干净的 XHTML,然后很容易解析。那么您不必担心 HTML 格式错误。我实际上只是在他提供的示例上运行了 Tidy HTML,使用在 Windows 中注册的TidyATL COM object(我确认它仍然可以在 64 位 win7 中工作,在 C:\Windows\SysWOW64 文件夹中注册)
【解决方案2】:

php的strip_tags函数呢?

烦人的部分是你必须传递每一个你想保存在数组中的标签,但你只需要写一次。

用于删除标签属性、bgcolor 等。有人制作了此功能here,这可能值得一看,但请注意该页面上的双引号。底部有一个链接可以下载没有wordpress格式的代码。

【讨论】:

    【解决方案3】:

    感谢@Paul 的想法,这里是 Excel 中的一个示例。这非常粗略,还需要根据您在 Excel 中存储 HTML 的方式进行修改;但希望它能让你开始。

    这个例子假设了一些事情:

    1. 您首先安装了TidyATL COM object(单击显示'wrapper' 的链接;您可以通过首先将DLL 复制到C:\Windows\SysWOW64 并运行regsvr32 C 在64 位Win 7 上注册它:\Windows\SysWOW64\TidyATL.dll)。

    2. 您的 Excel 项目引用了 Microsoft XML 6.0 和 Tidy 1.0 类型库

    3. 您的 HTML 存储在工作表 1 的单元格 A1 中。结果放在单元格 B1 中。您可以轻松地扩展此想法以遍历列中所有使用的单元格并一次处理所有 HTML。

    4. 我编写 XSLT 的经验为零。我直接从here 撕下了“身份模板”。在今天之前我从未使用过 XSLT。所以也许知道它的人可以编辑 XSLT 以去除 &lt;font&gt; 节点。这个例子只是去掉了所有的属性。

    这使用 Tidy HTML 将丑陋的 HTML 转换为 XHTML,然后将 XSLT 模板应用于结果。

    编辑:抱歉,搞砸了 XSLT 中的“匹配”属性。是:match='@*|node()' 应该是:match='node()'

    这是我使用的代码:

    Sub TidyUp()
    
        Dim t As TidyATL.TidyDocument
    
        Dim sXSLT
    
        sXSLT = "<?xml version='1.0' encoding='ISO-8859-1'?>" & _
            "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" & _
            "<xsl:template match='node()'>" & _
            "  <xsl:copy>" & _
            "    <xsl:apply-templates select='node()'/>" & _
            "  </xsl:copy>" & _
            "</xsl:template>" & _
            "</xsl:stylesheet>"
    
        Set t = New TidyATL.TidyDocument
        t.ParseString Sheet1.Range("A1").Value
        t.SetOptBool TidyXmlOut, True
        t.SetOptBool TidyXhtmlOut, True
        t.SetOptBool TidyNumEntities, True
        t.SetOptBool TidyXmlDecl, True
    
        t.CleanAndRepair
    
    
        Dim x As MSXML2.DOMDocument
        Dim x2 As MSXML2.FreeThreadedDOMDocument
        Dim xe As MSXML2.IXMLDOMParseError
        Set x = New MSXML2.DOMDocument
        Set x2 = New MSXML2.FreeThreadedDOMDocument
    
        'Load XHTML into a DOM
        x.LoadXML t.SaveString
        Set xe = x.parseError
        If xe.ErrorCode <> 0 Then
            MsgBox "Err: " & xe.reason
            End
        End If
    
        'Load XSLT into a DOM
        x2.LoadXML sXSLT
        Set xe = x2.parseError
        If xe.ErrorCode <> 0 Then
            MsgBox "Err: " & xe.reason
            End
        End If
    
    
        Dim xt As XSLTemplate
        Set xt = New XSLTemplate
        Set xt.stylesheet = x2
    
        Dim xp As IXSLProcessor
    
        Set xp = xt.createProcessor
        xp.input = x
        xp.transform
    
        Sheet1.Range("B1").Value = xp.output
    End Sub
    

    这是结果(仍然丑陋但没有属性):

    <?xml version="1.0" encoding="UTF-16"?><html xmlns="http://www.w3.org/1999/xhtml"><head><meta></meta><title></title></head><body><div><table><tbody><tr><td><p><font><b>Mont
    Blanc Scott Roof mounted cycle bike carrier<br></br><br></br>
     Part Number: 728540</b></font></p></td><td><a><img></img></a></td></tr><tr><td><b><font><script>
    //
        &lt;!--function click() { if (event.button==2) { alert('All graphics, descriptions and other information, including the HTML code of this listing are the property of XXXX Limited and may not be reproduced in any form without the express permission of XXXX Limited. Email us: sales@XXXX.com'); } } document.onmousedown=click // --&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt;
        &lt;!----&gt; --&gt;
    //</script></font></b><div><center><table><tbody><tr><td><p><img></img></p></td><td><p><font><u><strong>Mont Blanc</strong></u></font><u><strong><font>Scott Roof
    Bar Rack 1 Cycle Carrier</font></strong></u></p></td><td><img></img></td></tr><tr><td><hr></hr><p><img></img></p><p><font><b>Scott</b></font></p><ul><li>Stylish, easy to use roof mounted cycle carrier, distinctive
    oval carrying bar.<br></br></li><li>Extra Soft Frame clamps hold cycle safely and gently<br></br></li><li>Extra wide wheel holders take the fattest tyres<br></br></li><li>Strong Webbing straps fasten wheels securely to
    carrier<br></br></li><li><font>Upright, roof bar mounted, locking
    cycle carrier<br></br></font></li><li><font> Locks to roof rails and
    locks bikes<br></br></font></li><li><font> Quick and easy to
    use<br></br></font></li><li><font>Adjustable for most cycle
    styles</font></li></ul><center><table><tbody><tr><td><p><a><img></img></a></p></td><td>To view Fitting Instructions in
    PDF format please click the spanner</td></tr></tbody></table><table><tbody><tr><td><font>Technical data</font></td><td><p><font>Mont</font> Blanc Scott</p><p><img></img></p></td></tr><tr><td><div>Max number of bikes</div></td><td><div>1</div></td></tr><tr><td><div>Load capacity (kg)</div></td><td><div>15 KG</div></td></tr><tr><td><div>Weight (kg)</div></td><td><div>2.2KG</div></td></tr><tr><td><div>Fits frame-dimensions (mm)</div></td><td>Up to 80mm</td></tr><tr><td><div>Fits wheel-dimensions</div></td><td><div>All</div></td></tr><tr><td><div>Locks bikes to carrier</div></td><td><div>Yes</div></td></tr><tr><td><div>Locks carrier to car</div></td><td><div>Yes</div></td></tr><tr><td><div>Tilt function, with bikes</div></td><td><div>NA</div></td></tr><tr><td><div>TÜV/EuroBE approved</div></td><td><div>NA</div></td></tr><tr><td><div>Fullfills City Crash norms</div></td><td><div>NA</div></td></tr><tr><td><div>Miscellaneous</div></td><td><div><p>Fits all types of Roof Bars,</p></div></td></tr></tbody></table><p><f
    ont>The cycle carrier
    is guaranteed for Five year from date of purchase.<br></br><br></br>
     We stock a wide range of towbars and towing accessories.
    <a><br></br>
    Click here to email us</a> if you require details of our other
    towing equipment.</font></p><hr></hr></center></td></tr></tbody></table></center></div><b><br></br>
     Please note that with the Type of cycle carrier where you mount
    it<br></br>
     onto a flange ball you may need the long reach ball which
    will<br></br>
     allow you enough clearance from the bumper</b></td></tr><tr><td><a><img></img></a><b><font>Not from the UK ? Click
       the flag to purchase this item from our EU site</font></b><a><img></img></a></td></tr></tbody></table></div></body></html>
    

    编辑: 这个 XSLT 似乎可以解决问题;它会删除一些标签 with 他们的内容,以及一些标签 without 他们的内容,无论你指定什么。同样,也许有一些 XSLT 知识的人可以详细说明。

    <?xml version='1.0' encoding='ISO-8859-1'?>
    <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xhtml="http://www.w3.org/1999/xhtml" >
    
    <xsl:template match='node()|@*'>
      <xsl:copy>
        <xsl:apply-templates select='node()'/>
      </xsl:copy>
    </xsl:template>
    
    <!--these tags will be removed with their content-->
    <xsl:template match='xhtml:script|xhtml:head'/>
    
    <!--these tags will be removed but keep their content-->
    <xsl:template match='xhtml:font|xhtml:p|xhtml:b|xhtml:u|xhtml:i|xhtml:center|xhtml:a|xhtml:img|xhtml:strong'><xsl:apply-templates/></xsl:template>
    </xsl:stylesheet>
    

    结果:

    <?xml version="1.0" encoding="UTF-16"?><html xmlns="http://www.w3.org/1999/xhtml"><body><div><table><tbody><tr><td>Mont
    Blanc Scott Roof mounted cycle bike carrier<br></br><br></br>
     Part Number: 728540</td><td></td></tr><tr><td><div><table><tbody><tr><td></td><td>Mont BlancScott Roof
    Bar Rack 1 Cycle Carrier</td><td></td></tr><tr><td><hr></hr>Scott<ul><li>Stylish, easy to use roof mounted cycle carrier, distinctive
    oval carrying bar.<br></br></li><li>Extra Soft Frame clamps hold cycle safely and gently<br></br></li><li>Extra wide wheel holders take the fattest tyres<br></br></li><li>Strong Webbing straps fasten wheels securely to
    carrier<br></br></li><li>Upright, roof bar mounted, locking
    cycle carrier<br></br></li><li> Locks to roof rails and
    locks bikes<br></br></li><li> Quick and easy to
    use<br></br></li><li>Adjustable for most cycle
    styles</li></ul><table><tbody><tr><td></td><td>To view Fitting Instructions in
    PDF format please click the spanner</td></tr></tbody></table><table><tbody><tr><td>Technical data</td><td>Mont Blanc Scott</td></tr><tr><td><div>Max number of bikes</div></td><td><div>1</div></td></tr><tr><td><div>Load capacity (kg)</div></td><td><div>15 KG</div></td></tr><tr><td><div>Weight (kg)</div></td><td><div>2.2KG</div></td></tr><tr><td><div>Fits frame-dimensions (mm)</div></td><td>Up to 80mm</td></tr><tr><td><div>Fits wheel-dimensions</div></td><td><div>All</div></td></tr><tr><td><div>Locks bikes to carrier</div></td><td><div>Yes</div></td></tr><tr><td><div>Locks carrier to car</div></td><td><div>Yes</div></td></tr><tr><td><div>Tilt function, with bikes</div></td><td><div>NA</div></td></tr><tr><td><div>TÜV/EuroBE approved</div></td><td><div>NA</div></td></tr><tr><td><div>Fullfills City Crash norms</div></td><td><div>NA</div></td></tr><tr><td><div>Miscellaneous</div></td><td><div>Fits all types of Roof Bars,</div></td></tr></tbody></table>The cycle carrier
    is guaranteed for Five year from date of purchase.<br></br><br></br>
     We stock a wide range of towbars and towing accessories.
    <br></br>
    Click here to email us if you require details of our other
    towing equipment.<hr></hr></td></tr></tbody></table></div><br></br>
     Please note that with the Type of cycle carrier where you mount
    it<br></br>
     onto a flange ball you may need the long reach ball which
    will<br></br>
     allow you enough clearance from the bumper</td></tr><tr><td>Not from the UK ? Click
       the flag to purchase this item from our EU site</td></tr></tbody></table></div></body></html>
    

    【讨论】:

      【解决方案4】:

      这个正则表达式应该会给你预期的结果,但我还没有测试过:

      preg_replace('/(<.*)(style=\".*\")(.*>)/', '{$1}{$3}', $yourhtml);
      

      【讨论】:

      • 字体标签和背景属性怎么样。我认为这适用于样式属性。
      【解决方案5】:

      我认为所需的正则表达式可能比您想象的要简单得多,但话又说回来,我不知道产品描述是什么样的。除了作为 HTML 标记的一部分之外,在描述中遇到 的可能性有多大?如果机会很小,这样的事情能不能解决问题?

      $new_description = preg_replace('/<([\w_ '"])+>/', '', $description);
      

      【讨论】:

        猜你喜欢
        • 2021-08-19
        • 1970-01-01
        • 2011-07-27
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        • 2010-10-02
        • 2014-10-08
        • 1970-01-01
        相关资源
        最近更新 更多