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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:58:03+00:00 2026-06-13T22:58:03+00:00

I’m trying to pass a block of HTML through PHP’s DOMDocument to refactor <img>

  • 0

I’m trying to pass a block of HTML through PHP’s DOMDocument to refactor <img> elements inline with: http://www.appelsiini.net/projects/lazyload

For each <img> I’m trying to insert a refactored duplicate, then wrap the original in <noscript> tags for a non-JS fallback.

Example:

Input:

<img src="img/example.jpg" width="640" heigh="480">

Output:

<img class="lazy" src="img/grey.gif" data-original="img/example.jpg" width="640" heigh="480"><noscript><img src="img/example.jpg" width="640" heigh="480"></noscript>

I have the wrapping working and the new element also constructs correctly. The problem is that the second appendChild (below) doesn’t work and I can’t figure out why. Can anyone provide a solution?

I have the following code:

$noScript = $dom->createElement('noscript');
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
    $src = $image->getAttribute('src');
    $alt = $image->getAttribute('alt');
    $title = $image->getAttribute('title');
    $style = $image->getAttribute('style');

    $newImage = $dom->createElement('img');
    $newImage->setAttribute('class', 'lazy');
    $newImage->setAttribute('data-original', $src);
    $newImage->setAttribute('src', '/images/whitespace.gif');
    if ($style) $newImage->setAttribute('style', $style);
    if ($alt) $newImage->setAttribute('alt', $alt);
    if ($title) $newImage->setAttribute('title', $title);

    $noScriptClone = $noScript->cloneNode();
    $image->parentNode->replaceChild($noScriptClone, $image);
    $noScriptClone->appendChild($image);
    $noScriptClone->parentNode->appendChild($newImage);
}
return $dom->saveHTMLExact();
  • 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-13T22:58:04+00:00Added an answer on June 13, 2026 at 10:58 pm

    I did solve this (in a roundabout kind of way) but forgot to post my solution – until now.

    I ended up using QueryPath which worked out really well.

    function lazyLoadImages($source = '') {
      $qp = htmlqp($source);
      foreach ($qp->find('img') as $image) {
        $src = $image->attr('src');
        $alt = $image->attr('alt');
        $title = $image->attr('title');
        $style = $image->attr('style');
        $class = $image->attr('class');
        $image->wrap('<noscript/>')->parent('noscript')->after('<img src="/images/whitespace.gif" data-original="' . $src . '" style="' . trim($style) . '"' . ($alt ? ' alt="' . $alt . '"' : '') . ($title ? ' title="' . $title . '"' : '') . ' class="lazy' . ($class ? ' ' . $class : '') . '" />');
      }
      $content = preg_replace(array("/<\?xml.*?\?>/si", "/<\!DOCTYPE.*?>/", "!<(/)?body>!si", "!<(/)?html>!si"), '', $qp->writeSnippet());
      return $content;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

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.