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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:51:30+00:00 2026-06-13T23:51:30+00:00

Possible Duplicate: PHP – How to replace a phrase with another? I need to

  • 0

Possible Duplicate:
PHP – How to replace a phrase with another?

I need to transform this piece of html

<html>
   <body>
      Lorum ipsum <a href="http://google.com">click here</a> dolores lorem.
      Lorum ipsum <a href="http://stackoverflow.com">click here too</a> dolores lorem.
   </body>
</html>

To:

<html>
    <body>
       Lorum ipsum @@1@@ dolores lorem.
       Lorum ipsum @@2@@ dolores lorem.
    </body>
</html>

How can this be achieved using Domdocument (I’m not interested in een regex solution)?

  • 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-13T23:51:31+00:00Added an answer on June 13, 2026 at 11:51 pm

    You have not shown any code so far so it is not really clear into which problem you run. I can only assume that it is because if you iterate over the list of links changing it, the iteration will get invalid. So only the first element is being replaced.

    Using a for loop can help here only getting the first a element per each iteration. It would also allow to initialize and increase the count variable for the numbers you need in the replacement.

    The replacement itself can be easily done with replaceChild. The loop example:

    for($c = 1; $a = $doc->getElementsByTagName('a')->item(0); $c++) {
        $a->parentNode->replaceChild(
            $doc->createTextNode(sprintf("@@%d@@", $c)),
            $a
        );
    }
    

    The call to $doc->getElementsByTagName('a')->item(0) will return NULL if no such element exists (any longer). That is the exit condition of the loop.

    Full example:

    $html = '<html><body>
      Lorum ipsum <a href="http://google.com">click here</a> dolores lorem.
      Lorum ipsum <a href="http://stackoverflow.com">click here too</a> dolores lorem.
    </body></html>';
    
    $doc = new DOMDocument();
    $doc->loadHtml($html);
    
    for($c = 1; $a = $doc->getElementsByTagName('a')->item(0); $c++) {
        $a->parentNode->replaceChild(
            $doc->createTextNode(sprintf("@@%d@@", $c)),
            $a
        );
    }
    
    echo $doc->saveHTML();
    

    Output:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html><body>
      Lorum ipsum @@1@@ dolores lorem.
      Lorum ipsum @@2@@ dolores lorem.
    </body></html>
    

    I hope this is helpful.

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

Sidebar

Related Questions

Possible Duplicate: php loop through associative arrays i have this piece of code which
Possible Duplicate: PHP - opendir on another server Something like this: opendir(http://super.cdn.com/running_order_image/sale/587/) The opendir
Possible Duplicate: PHP, Echoing an array into a string of comma seperated values This
Possible Duplicate: php regex , extract phone number from text/html I have following requirement.
Possible Duplicate: PHP: Require() and Class Hierarchies Is this bad practice? Is there any
Possible Duplicate: PHP: self vs. $this What does $this-> mean in CakePHP? Please answer
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: php validate integer Currently I'm using this method: Validate the input using
Possible Duplicate: PHP global in functions Using something like this worries me: <? global

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.