Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3337134
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:13:04+00:00 2026-05-18T00:13:04+00:00

The script below takes the $myContent block and does the doReplace() function on the

  • 0

The script below takes the $myContent block and does the doReplace() function on the $myKeyword value. The problem I have is that it does not know if the replacement text is already inside a tag or not.

I need to modify the doReplace() function so that it does not touch any text inside of or as attributes of the named tags (h1, h2, h3, i, u, b, strong, em, img).

I think it would be better to convert this over to an xPath method and looking for suggestions on how it might be done with xPath. So the question is: “How would you convert this to xPath”?

Note: The count variable in the replace function is there because I’m only replacing on the first three appearances of the keyword. First time it appears it gets bolded, next time it gets italicized and the third time it appears, it gets underlined.

$myKeyword = "test keyword";

$myContent = "My content contains the "test keyword". 
Don't do the replace if the test keyword is inside:
h1, h2, h3, i, u, b, strong, em, tags. 
<h1>This test keyword would not be replaced</h1>";

$myContent = preg_replace_callback("/\b($mykeyword)\b/i","doReplace", $myContent);

function doReplace($matches)
{
    static $count = 0;
    switch($count++) {
    case 0: return ' <b>'.trim($matches[1]).'</b>';
    case 1: return ' <em>'.trim($matches[1]).'</em>';
    case 2: return ' <u>'.trim($matches[1]).'</u>';
    default: return $matches[1];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:13:05+00:00Added an answer on May 18, 2026 at 12:13 am

    You can’t in XPath 1.0 nor 2.0, because you need recursion to express this algorithm. You could use an extension function, of course.

    This XQuery:

    declare variable $match as xs:string external;
    declare variable $replace as xs:string external;
    declare variable $preserve as xs:string external;
    declare variable $vPreserve := tokenize($preserve,',');
    declare function local:copy-replace($element as element()) {   
       element {node-name($element)}
               {$element/@*,  
                for $child in $element/node()   
                    return if ($child instance of element())   
                           then local:copy-replace($child)   
                           else if ($child instance of text() and
                                    not(name($element)=$vPreserve))
                                then replace($child,$match,$replace)
                                else $child
               }
    };   
    local:copy-replace(/*) 
    

    With this input:

    <html>
        <h1>This test keyword would not be replaced</h1>
        <p>This test keyword should be replaced</p>
    </html>
    

    Output:

    <html>
        <h1>This test keyword would not be replaced</h1>
        <p>This replaced should be replaced</p>
    </html>
    

    Also this stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml"/>
        <xsl:param name="pMatch" select="'test keyword'"/>
        <xsl:param name="pReplace" select="'replaced'"/>
        <xsl:param name="pPreserve" select="'h1,h2,h3,i,u,b,strong,em'"/>
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="text()">
            <xsl:choose>
                <xsl:when test="contains(concat(',',$pPreserve,','),
                                         concat(',',name(..),','))">
                    <xsl:value-of select="."/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="replace"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
        <xsl:template name="replace">
            <xsl:param name="pString" select="."/>
            <xsl:choose>
                <xsl:when test="contains($pString,$pMatch)">
                    <xsl:value-of
                         select="concat(substring-before($pString,
                                                         $pMatch),
                                        $pReplace)"/>
                    <xsl:call-template name="replace">
                        <xsl:with-param name="pString"
                                        select="substring-after($pString,
                                                                $pMatch)"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$pString"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    

    EDIT: Better XQuery.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script which nests foreach loops as seen below. It takes
I have to create a script that takes a mySQL table, and exports it
I have a script that uses Parallel::ForkManager . However, the wait_all_children() process takes incredibly
I have written a pretty basic upload script that takes the file and uploads
I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),
I have a script that retrieves objects from a remote server through an Ajax
I have a Perl script that I'm attempting to set up using Perl Threads
I have an Ant script that performs a copy operation using the 'copy' task
I have two problems in my current .htaccess. The script below works but has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.