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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:21:14+00:00 2026-06-10T01:21:14+00:00

EDIT : related to PHP HTML DomDocument getElementById problems Can’t get getElementById() to work.

  • 0

EDIT: related to PHP HTML DomDocument getElementById problems

Can’t get getElementById() to work. Document is valid, according to using W3C validator. Plus my code should work even with a little invalid HTML.

This simple code looks for an image with id="banner" and replace it’s src attribute with another one. Works on my development machine (Windows), doesn’t work on server (Ubuntu).

Any idea of how to do this without getElementById()?

    libxml_use_internal_errors(true);

    // Create the DOMDocument and get the HTML content
    $document = new \DOMDocument();

    // Load HTML string and if it fails just return the content itself
    if(false === $document->loadHTML($content)) return $content;

    // Get DOMElement of the image with id="banner"
    $img = $document->getElementById('banner');

    // Return the content if it can't find the image
    if(null === $img) return $content;

    // Get image parent and remove the banner from DOM
    $parent = $img->parentNode;
    $parent->removeChild($img);

    // Set the new src attribute
    $img->setAttribute('src', 'http://mysite.come/img/myimage.png');

    // Append the modified node to banner parent
    $parent->appendChild($img);

    return $document->saveHTML();
  • 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-10T01:21:15+00:00Added an answer on June 10, 2026 at 1:21 am

    Got it, don’t know how it’s invalid-document-proof:

    $xpath = new \DOMXpath($document);
    $nodes = $xpath->query('//img[@id="banner"]');
    
    // Return content if we don't have exactly one image with id="banner"
    if(1 !== $nodes->length) return $content;
    
    // DOMNode of the banner
    $banner = $nodes->item(0);
    
    // Set the new src attribute and save the content
    $banner->setAttribute('src', 'http://mysite.come/img/myimage.png');
    $banner->ownerDocument->saveXML($banner);
    
    return $document->saveXML();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I'm using Vim 7.3 to edit a large PHP file (2000 lines), if
I am using a dynamic HTML template system (like PHP) and I'm wondering if
I work on two related web applications, developed in PHP, that started independently. Over
This is related to my previous question I'm solving UVA's Edit Step Ladders and
I need the form that will edit the array of entities and their related
EDIT: iam using ajax to load text in my content that is why onload
EDIT : It turned out that this can only be done through an external
So we have a real-estate-related site that has controller/action pairs like homes/view, realtors/edit, and
Is there a way to set a $_POST['var'] without using form related field (no
I am trying create a 'live' view where our clients can edit the colors

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.