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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:55:58+00:00 2026-05-23T12:55:58+00:00

I’m stuck on the following problem and would like to know if you got

  • 0

I’m stuck on the following problem and would like to know if you got an advise.

A WYSIWYG editor allows the user to upload and embed images. However, my users are mostly scientists but don’t have any knowledge of how to use HTML or even how to re-size images properly for a web page. That’s why I am re-sizing the images automatically server-side to a thumbnail and a full view size. Clicking on a thumbnail shall open a lightbox with full image.

The WYSIWYG editor throws images into <p> tags just like this (see last paragraph):

<p>Intro Text</p>
<ul>
   <li>List point 1</li>
   <li>List point 2</li>
</ul>
<p>Some text before an image. 
   <img alt="Slide 1" src="/files/slide1.png" /> 
   Maybe some text in between, nobody knows what the scientists are up to. 
   <img alt="Slide 2" src="/files/slide2.png" /> 
   And even more text right after that.
</p>

What I would like to do is get the images out of the <p> Tags and add them before the respective paragraph within floating <div>s:

<p>Intro Text</p>
<ul>
   <li>List point 1</li>
   <li>List point 2</li>
</ul>
<div class="custom">
   <a href="/files/fullview/slide1.png" rel="lightbox[group][Slide 1]">
      <img src="/files/thumbs/files/slide1.png" />
   </a>
</div>
<div class="custom">
   <a href="/files/fullview/slide2.png" rel="lightbox[group][Slide 2]">
      <img src="/files/thumbs/files/slide2.png" />
   </a>
</div>
<p>Some text before an image. 
   Maybe some text in between, nobody knows what the scientists are up to. 
   And even more text right after that.
</p>

So what I need to do is to get all the image nodes of the html produced by the editor, process them, insert the divs and remove the image nodes.
After reading quite a lot of similar questions I’m missing something and can’t get it to work. Probably, I am still misunderstanding the whole concept behind DOM manipulation.
Here’s what I came up with til now:

// create DOMDocument
$doc = new DOMDocument();
// load WYSIWYG html into DOMDocument
$doc->loadHTML($html_from_editor);
// create DOMXpath
$xpath = new DOMXpath($doc);
// create list of all first level DOMNodes (these are p's or ul's in most cases)
$children = $xpath->query("/");
foreach ( $children AS $child ) {
    // now get all images
    $cpath = new DOMXpath($child);
    $images = $cpath->query('//img');
    foreach ( $images AS $img ) {
        // get attributes
        $atts = $img->attributes;
        // create replacement
        $lb_div = $doc->createElement('div');
        $lb_a = $doc->createElement('a');
        $lb_img = $doc->createElement('img');
        $lb_img->setAttribute("src", '/files/thumbs'.$atts->src);
        $lb_a->setAttribute("href", '/files/fullview'.$atts->src);
        $lb_a->setAttribute("rel", "lightbox[slide][".$atts->alt."]");
        $lb_a->appendChild($lb_img);
        $lb_div->setAttribute("class", "custom");
        $lb_div->appendChild($lb_a);
        $child->insertBefore($lb_div);
        // remove original node
        $child->removeChild($img);
    }
}

Problems I ran into:

  1. `$atts` is not populated with values. It does contain the right attribute names, but values are missing.
  2. `insertBefore` should be called on the child’s parent node if I understood that right. So, it should rather be `$child->parentNode->insertBefore($lb_div, $child);` but the parent node is not defined.
  3. Removal of original img tag does not work.

I’d be thankful for any advise what I am missing. Am I on the right track or should this be done completely different?

Thans in advance,
Paul

  • 1 1 Answer
  • 3 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-23T12:55:58+00:00Added an answer on May 23, 2026 at 12:55 pm

    As I commented, your code had multiple errors which prevented you from getting started. Your concept looks quite well from what I see and the code itself only had minor issues.

    1. You were iterating over the document root element. That’s just one element, so picking up all images therein.
    2. The second xpath must be relative to the child, so starting with ..
    3. If you load in a HTML chunk, DomDocument will create the missing elements like body around it. So you need to address that for your xpath queries and the output.
    4. The way you accessed the attributes was wrong. With error reporting on, this would have given you error information about that.

    Just take a look through the working code I was able to assemble (Demo). I’ve left some notes:

    $html_from_editor = <<<EOD
    <p>Intro Text</p>
    <ul>
       <li>List point 1</li>
       <li>List point 2</li>
    </ul>
    <p>Some text before an image. 
       <img alt="Slide 1" src="/files/slide1.png" /> 
       Maybe some text in between, nobody knows what the scientists are up to. 
       <img alt="Slide 2" src="/files/slide2.png" /> 
       And even more text right after that.
    </p>
    EOD;
    
    // create DOMDocument
    $doc = new DOMDocument();
    // load WYSIWYG html into DOMDocument
    $doc->loadHTML($html_from_editor);
    // create DOMXpath
    $xpath = new DOMXpath($doc);
    
    // create list of all first level DOMNodes (these are p's or ul's in most cases)
    # NOTE: this is XHTML now
    $children = $xpath->query("/html/body/p");
    
    foreach ( $children AS $child ) {
        // now get all images
        $cpath = new DOMXpath($doc);
        $images = $cpath->query('.//img', $child); # NOTE relative to $child, mind the .
    
        // if no images are found, continue
        if (!$images->length) continue;
    
        // insert replacement node
        $lb_div = $doc->createElement('div');
        $lb_div->setAttribute("class", "custom");
        $lb_div = $child->parentNode->insertBefore($lb_div, $child);
    
    
        foreach ( $images AS $img ) {
            // get attributes
            $atts = $img->attributes;
            $atts = (object) iterator_to_array($atts); // make $atts more accessible    
    
            // create the new link with lighbox and full view
            $lb_a = $doc->createElement('a');
            $lb_a->setAttribute("href", '/files/fullview'.$atts->src->value);
            $lb_a->setAttribute("rel", "lightbox[slide][".$atts->alt->value."]");
    
            // create the new image tag for thumbnail
            $lb_img = $img->cloneNode(); # NOTE clone instead of creating new
            $lb_img->setAttribute("src", '/files/thumbs'.$atts->src->value);
    
            // bring the new nodes together and insert them
            $lb_a->appendChild($lb_img);
            $lb_div->appendChild($lb_a);
    
            // remove the original image
            $child->removeChild($img);
        }
    }
    
    // get body content (original content)
    $result = '';
    foreach ($xpath->query("/html/body/*") as $child) {
        $result .= $doc->saveXML($child); # NOTE or saveHtml 
    }
    
    echo $result;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.