douleng3463 2010-11-05 22:36
浏览 59
已采纳

DOM node.textContent解析和替换

In the foreach below, I need to parse the $node.TextContent for the keyword and wrap bold tags around it. Is this possible with DOM? How?

$myContent ="<h1>This word should not be replaced: TEST</h1>. But this one should be replaced: test";

$dom = new DOMDocument;
$dom->loadHTML(strtolower($myContent));
$xPath = new DOMXPath($dom);
foreach($xPath->query("//text()[contains(.,'test') and not(ancestor::h1)]") as $node)
    {
        /*need to do a replace on each occurrence of the word "test"
         in $node->textContent here so that it becomes <b>test</b>. How? */
    }

echo $dom->saveHTML should yield:

<h1>This word should not be replaced: TEST</h1>. 
But this one should be replaced: <b>test</b>"
  • 写回答

1条回答 默认 最新

  • duan0708676887 2010-11-05 23:10
    关注

    Edit: As @LarsH has noticed, I hadn't paid attention to the requirement that the replacement should be in bold.

    There are two easy ways to correct this:

    .1. In the transformation replace:

      <xsl:value-of select="$pRep"/>
    

    with

      <b><xsl:value-of select="$pRep"/></b>
    

    .2. Pass as the value of the pReplacement parameter not just "ABC" but <b>ABC</b>

    This XSLT 1.0 transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pTarget" select="'test'"/>
     <xsl:param name="pReplacement" select="'ABC'"/>
    
     <xsl:variable name="vCaps"     select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
     <xsl:variable name="vLowecase" select="'abcdefghijklmnopqrstuvwxyz'"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="text()[not(ancestor::h1)]">
      <xsl:call-template name="replaceCI">
       <xsl:with-param name="pText" select="."/>
      </xsl:call-template>
     </xsl:template>
    
     <xsl:template name="replaceCI">
      <xsl:param name="pText"/>
      <xsl:param name="pTargetText" select="$pTarget"/>
      <xsl:param name="pRep" select="$pReplacement"/>
    
      <xsl:variable name="vLowerText"
           select="translate($pText, $vCaps, $vLowecase)"/>
      <xsl:choose>
       <xsl:when test=
       "not(contains($vLowerText, $pTargetText))">
         <xsl:value-of select="$pText"/>
       </xsl:when>
       <xsl:otherwise>
        <xsl:variable name="vOffset" select=
        "string-length(substring-before($vLowerText, $pTargetText))"/>
    
        <xsl:value-of select="substring($pText,1,$vOffset)"/>
    
        <xsl:value-of select="$pRep"/>
    
        <xsl:call-template name="replaceCI">
         <xsl:with-param name="pText" select=
         "substring($pText, $vOffset + string-length($pTargetText)+1)"/>
         <xsl:with-param name="pTargetText" select="$pTargetText"/>
         <xsl:with-param name="pRep" select="$pRep"/>
        </xsl:call-template>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document (corrected to be well-formed):

    <html>
    <h1>This word should not be replaced: TEST</h1>.
     But this one should be replaced: test
    </html>
    

    produces the wanted result:

    <html>
    <h1>This word should not be replaced: TEST</h1>.
     But this one should be replaced: ABC
    </html>
    

    Do note:

    1. This is a generic transformation that accepts as parameters the target and the replacement text.

    2. The replacement is case-incensitive, but we suppose that the target parameter is provided in lowercase.

    3. It is even much easier to solve this with XSLT 2.0.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)