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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:11:32+00:00 2026-06-07T16:11:32+00:00

In my piece of code I’m trying to find all img tags using PHP

  • 0

In my piece of code I’m trying to find all img tags using PHP DOM, add another img tag directly after and then wrap all of that in a div, i.e.

<!-- From this... -->
<img src="originalImage.jpg" />

<!-- ...to this... -->
<div class="wrappingDiv">
    <img src="originalImage.jpg" />
    <img src="newImage.jpg" />
</div>

This is the PHP that I’m bastardising trying:

$dom = new domDocument;
$dom->loadHTML($the_content_string);
$dom->preserveWhiteSpace = false;
    //get all images and chuck them in an array
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
            //create the surrounding div
    $div = $image->ownerDocument->createElement('div');
    $image->setAttribute('class','main-image');
        $added_a = $image->parentNode->insertBefore($div,$image);
        $added_a->setAttribute('class','theme-one');
        $added_a->appendChild($image);
            //create the second image
    $secondary_image = $image->ownerDocument->createElement('img');
    $added_img = $image->appendChild($secondary_image);
    $added_img->setAttribute('src', $twine_img_url);
    $added_img->setAttribute('class', $twine_class);
    $added_img->appendChild($image);
    }

echo $dom->saveHTML();

Everything up to where I create the $added_img variable works fine. Well, at the very least it doesn’t error. It’s those last four lines that kill it dead.

I’m clearly doing something relatively idiotic… Any lovely, lovely people out there able to point out where I’ve douched things up?

  • 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-07T16:11:35+00:00Added an answer on June 7, 2026 at 4:11 pm

    First:

    You are trying to append and image to an image here(but of course in HTML an image cannot have an child-image, append the images to the div):

    $added_img = $image->appendChild($secondary_image);
    

    it has to be

    $added_img = $added_a->appendChild($secondary_image);
    

    and again here:

    $added_img->appendChild($image);
    

    has to be:

    $added_a->appendChild($image);
    

    But this will not work at all, because NodeList’s are live. As soon as you append one new image, this image is a part of $images, you will run into an infinite loop. So first populate an array with the initial images instead of using a NodeList.

    $imageList= $dom->getElementsByTagName('img');
    $images=array();
    for($i=0;$i<$imageList->length;++$i)
    {
      $images[]=$imageList->item($i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This piece of code is working fine with normal php: <?php function barber($type) {
This piece of code grabs a customers hire record using their hire ID and
The piece of code below takes a string (userstring) and searches all .txt and
this piece of code is driving me crazy. I'm trying to print a help
This piece of code doesn't work; it's logging in into website which is using
a piece of code here jmp_buf mark; int Sub_Func() { int be_modify, jmpret; be_modify
First piece of code: // code is a private global variable for the class
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
This piece of code is from Adobe docs : package { import flash.display.Loader; import
This piece of code is supposed to go through a list and preform some

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.