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 9014629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:33:19+00:00 2026-06-16T03:33:19+00:00

I have html content stored in a database table. in that html content I

  • 0

I have html content stored in a database table. in that html content I want to replace “SOME WORDS” with a link tag. But if “SOME WORDS” is already inside a link tag i should omit them..

e.g.
The content

<p>Lorem ipsum dolor SOME WORDS, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>

The output should be

<p>Lorem ipsum dolor <a href="http://someurl">SOME WORDS</a>, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>

as you can see, it should exclude existing link texts when replacing.

Some guidance to get in to the right track is very much appreciated.

  • 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-06-16T03:33:20+00:00Added an answer on June 16, 2026 at 3:33 am

    This is how you could solve it using DOMDocument instead of regular expressions:

    $contents = <<<EOS
    <p>Lorem ipsum dolor SOME WORDS, consectetur adipiscing elit. <a href="http://example.com">SOME WORDS</a> elementum pharetra velit at cursus. Quisque blandit, nibh at eleifend ullamcorper</p>
    EOS;
    
    $doc = new DOMDocument;
    libxml_use_internal_errors(true);
    $doc->loadHTML($contents);
    libxml_clear_errors();
    
    $xp = new DOMXPath($doc);
    
    // find all text nodes
    foreach ($xp->query('//text()') as $node) {
            // make sure it's not inside an anchor
            if ($node->parentNode->nodeName !== 'a') {
                    $node->nodeValue = str_replace(
                        'SOME WORDS', 
                        'SOME OTHER WORDS', 
                        $node->nodeValue
                    );
            }
    }
    // DOMDocument creates a full document and puts your fragment inside a body tag
    // So we enumerate the children and save their HTML representation
    $body = $doc->getElementsByTagName('body')->item(0);
    foreach ($body->childNodes as $node) {
            echo $doc->saveHTML($node);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some html content stored in a database field. I would like trusted
I have a database table having HTML content stored as binary serialized blob. I
I want to parse a html content that have something like this: <div id=sometext>Lorem<br>
I have content stored as raw text in a database that I show in
I have data stored in the database with HTML format , and I want
I have a php script that will parse html page content link and view
I want to store some Hindi words in a MySQL database. For that I
I have the following html content: <p>My name is <a href=way2project>way2project</a></p> Now I want
I have a JSON file which contains HTML content. I want to load it
I have an asp.net UserControl that writes HTML content from a SharePoint list to

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.