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

  • SEARCH
  • Home
  • 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 729165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:45:57+00:00 2026-05-14T06:45:57+00:00

I would like modify HTML like I am <b>Sadi, novice</b> programmer. to I am

  • 0

I would like modify HTML like

I am <b>Sadi, novice</b> programmer.

to

I am <b>Sadi, learner</b> programmer.

To do it I will search using a string “novice programmer“. How can I do it please? Any idea?

It search using more than one word “novice programmer”. It could be a whole sentence. The extra white space (e.g. new line, tab) should be ignored and any tag must be ignored during the search. But during the replacement tag must be preserved.

It is a sort of converter. It will be better if it is case insensitive.

Thank you

Sadi


More clarification:

I get some nice reply with possible solution. But please keep posting if you have any idea in mind.

I would like to more clarify the problem just in case anyone missed it. Main post shows the problem as an example scenario.

1) Now the problem is find and replace some string without considering the tags. The tags may shows up within a single word. String may contain multiple word. Tag only appear in the content string or the document. The search phrase never contain any tags.

We can easily remove all tags and do some text operation. But here the another problem shows up.

2) The tags must be preserve, even after replacing the text. That is what the example shows.

Thank you Again for helping

  • 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-14T06:45:57+00:00Added an answer on May 14, 2026 at 6:45 am

    ok i think this is what you want. it takes your input search and replace, splits them into arrays of strings delimited by space, generates a regexp that finds the input sentence with any number of whitespace/html tags, and replaces it with the replacement sentence with the same tags replaced between the words.

    if the wordcount of the search sentence is higher than that of the replacement, it just uses spaces between any extra words, and if the replacement wordcount is higher than the search, it will add all ‘orphaned’ tags on the end. it also handles regexp chars in the find and replace.

    <?php
    function htmlFriendlySearchAndReplace($find, $replace, $subject) {
        $findWords = explode(" ", $find);
        $replaceWords = explode(" ", $replace);
    
        $findRegexp = "/";
        for ($i = 0; $i < count($findWords); $i++) {
            $findRegexp .= preg_replace("/([\\$\\^\\|\\.\\+\\*\\?\\(\\)\\[\\]\\{\\}\\\\\\-])/", "\\\\$1", $findWords[$i]);
            if ($i < count($findWords) - 1) {
                $findRegexp .= "(\s?(?:<[^>]*>)?\s(?:<[^>]*>)?)";
            }
        }
        $findRegexp .= "/i";
    
        $replaceRegexp = "";
        for ($i = 0; $i < count($findWords) || $i < count($replaceWords); $i++) {
            if ($i < count($replaceWords)) {
                $replaceRegexp .= str_replace("$", "\\$", $replaceWords[$i]);
            }
            if ($i < count($findWords) - 1) {
                $replaceRegexp .= "$" . ($i + 1);
            } else {
                if ($i < count($replaceWords) - 1) {
                    $replaceRegexp .= " ";
                }
            }
        }
    
        return preg_replace($findRegexp, $replaceRegexp, $subject);
    }
    ?>
    

    here are the results of a few tests :

    Original : <b>Novice Programmer</b>
    Search : Novice Programmer
    Replace : Advanced Programmer
    Result : <b>Advanced Programmer</b>
    
    Original : Hi, <b>Novice Programmer</b>
    Search : Novice Programmer
    Replace : Advanced Programmer
    Result : Hi, <b>Advanced Programmer</b>
    
    Original : I am not a <b>Novice</b> Programmer
    Search : Novice Programmer
    Replace : Advanced Programmer
    Result : I am not a <b>Advanced</b> Programmer
    
    Original : Novice <b>Programmer</b> in the house
    Search : Novice Programmer
    Replace : Advanced Programmer
    Result : Advanced <b>Programmer</b> in the house
    
    Original : <i>I am not a <b>Novice</b> Programmer</i>
    Search : Novice Programmer
    Replace : Advanced Programmer
    Result : <i>I am not a <b>Advanced</b> Programmer</i>
    
    Original : I am not a <b><i>Novice</i> Programmer</b> any more
    Search : Novice Programmer
    Replace : Advanced Programmer
    Result : I am not a <b><i>Advanced</i> Programmer</b> any more
    
    Original : I am not a <b><i>Novice</i></b> Programmer any more
    Search : Novice Programmer
    Replace : Advanced Programmer
    Result : I am not a <b><i>Advanced</i></b> Programmer any more
    
    Original : I am not a Novice<b> <i> </i></b> Programmer any more
    Search : Novice Programmer
    Replace : Advanced Programmer
    Result : I am not a Advanced<b> <i> </i></b> Programmer any more
    
    Original : I am not a Novice <b><i> </i></b> Programmer any more
    Search : Novice Programmer
    Replace : Advanced Programmer
    Result : I am not a Advanced <b><i> </i></b> Programmer any more
    
    Original : <i>I am a <b>Novice</b> Programmer</i> too, now
    Search : Novice Programmer too
    Replace : Advanced Programmer
    Result : <i>I am a <b>Advanced</b> Programmer</i> , now
    
    Original : <i>I am a <b>Novice</b> Programmer</i>, now
    Search : Novice Programmer
    Replace : Advanced Programmer Too
    Result : <i>I am a <b>Advanced</b> Programmer Too</i>, now
    
    Original : <i>I make <b>No money</b>, now</i>
    Search : No money
    Replace : Mucho$1 Dollar$
    Result : <i>I make <b>Mucho$1 Dollar$</b>, now</i>
    
    Original : <i>I like regexp, you can do [A-Z]</i>
    Search : [A-Z]
    Replace : [Z-A]
    Result : <i>I like regexp, you can do [Z-A]</i>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to modify the string in a .h file with NAnt before
I have a String which I would like to modify in some way. For
I would like to modify a Spotlight metadata attribute of a file within my
I would like to know how to modify the below code to strip =20
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I would like to use a language that I am familiar with - Java,

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.