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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:15:36+00:00 2026-05-13T08:15:36+00:00

I’m trying to parse some HTML with DOM in PHP, but I’m having some

  • 0

I’m trying to parse some HTML with DOM in PHP, but I’m having some problems. First, in case this change the solution, the HTML that I have is not a full page, rather, it’s only part of it.

<!-- This is the HTML that I have --><a href='/games/'>
<div id='game'>
<img src='http://images.example.com/games.gif' width='300' height='137' border='0'>
<br><b> Game </b>
</div>
<div id='double'>
<img src='http://images.example.com/double.gif' width='300' height='27' border='0' alt='' title=''>
</div>
</a>

Now I’m trying to get only the div with the id double. I’ve tried the following code, but it doesn’t seem to be working properly. What might I be doing wrong?

//The HTML has been loaded into the variable $html
$dom=new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false; 
$keepme = $dom->getElementById('double'); 

$contents = '<div style="text-align:center">'.$keepme.'</a></div>';
echo $contents;
  • 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-05-13T08:15:36+00:00Added an answer on May 13, 2026 at 8:15 am

    I think DOMDocument::getElementById will not work in your case : (quoting)

    For this function to work, you will
    need either to set some ID attributes
    with DOMElement::setIdAttribute or a
    DTD which defines an attribute to be
    of type ID.
    In the later case, you
    will need to validate your document
    with DOMDocument::validate or
    DOMDocument->validateOnParse before
    using this function.

    A solution that might work is using some XPath query to extract the element you are looking for.

    First of all, let’s load the HTML portion, like you first did :

    $dom=new domDocument;
    $dom->loadHTML($html);
    var_dump($dom->saveHTML());
    

    The var_dump is here only to prove that the HTML portion has been loaded successfully — judging from its output, it has.

    Then, instanciate the DOMXPath class, and use it to query for the element you want to get :

    $xpath = new DOMXpath($dom);
    $result = $xpath->query("//*[@id = 'double']");
    $keepme = $result->item(0);
    

    We now have to element you want 😉

    But, in order to inject its HTML content in another HTML segment, we must first get its HTML content.

    I don’t remember any “easy” way to do that, but something like this sould do the trick :

    $tempDom = new DOMDocument();
    $tempImported = $tempDom->importNode($keepme, true);
    $tempDom->appendChild($tempImported);
    $newHtml = $tempDom->saveHTML();
    var_dump($newHtml);
    

    And… We have the HTML content of your double <div> :

    string '<div id="double">
    <img src="http://images.example.com/double.gif" width="300" height="27" border="0" alt="" title="">
    </div>
    ' (length=125)
    

    Now, you just have to do whatever you want with it 😉

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

Sidebar

Ask A Question

Stats

  • Questions 288k
  • Answers 288k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes. See: http://www.alistapart.com/articles/prettyaccessibleforms/ Some screen readers will even read the… May 13, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer You should probably use containment instead of inheritance. For example,… May 13, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer Go and buy this book: (source: filthyrichclients.org) From the description… May 13, 2026 at 5:19 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.