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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:20:48+00:00 2026-05-16T03:20:48+00:00

A little new to PHP parsing here, but I can’t seem to get PHP’s

  • 0

A little new to PHP parsing here, but I can’t seem to get PHP’s DOMDocument to return what is clearly an identifiable node. The HTML loaded will come from the ‘net so can’t necessarily guarantee XML compliance, but I try the following:

<?php
header("Content-Type: text/plain");

$html = '<html><body>Hello <b id="bid">World</b>.</body></html>';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = true;

/*** load the html into the object ***/
$dom->loadHTML($html);
var_dump($dom);    
    
$belement = $dom->getElementById("bid");
var_dump($belement);

?>

Though I receive no error, I only receive the following as output:

object(DOMDocument)#1 (0) {
}
NULL

Should I not be able to look up the <b> tag as it does indeed have an id?

  • 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-16T03:20:49+00:00Added an answer on May 16, 2026 at 3:20 am

    The Manual explains why:

    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.

    By all means, go for valid HTML & provide a DTD.

    Quick fixes:

    1. Call $dom->validate(); and put up with the errors (or fix them), afterwards you can use $dom->getElementById(), regardless of the errors for some reason.
    2. Use XPath if you don’t feel like validing: $x = new DOMXPath($dom); $el = $x->query("//*[@id='bid']")->item(0);
    3. Come to think of it: if you just set validateOnParse to true before loading the HTML, if would also work ;P

    .

    $dom = new DOMDocument();
    $html ='<html>
    <body>Hello <b id="bid">World</b>.</body>
    </html>';
    $dom->validateOnParse = true; //<!-- this first
    $dom->loadHTML($html);        //'cause 'load' == 'parse
    
    $dom->preserveWhiteSpace = false;
    
    $belement = $dom->getElementById("bid");
    echo $belement->nodeValue;
    

    Outputs ‘World’ here.

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

Sidebar

Related Questions

Parsing HTML / JS codes to get info using PHP. www.asos.com/Asos/Little-Asos-Union-Jack-T-Shirt/Prod/pgeproduct.aspx?iid=1273626 Take a look
So i am fairly new to PHP (worked with it a little, not much
hello i am little new to all this but i want to know that:
I'm a little new at PHP, so my script writing is still progressing. I
I am a little bit new to the PHP/MYSQL arena, and had an idea
I'm a little new to drupal but have been using things like devel module
I am new to Grails and beyond a little bit of PHP have little
I am somewhat new to PHP's OO system and the little quirks. From what
I'm still pretty new to php so I'm a little shaky on getting the
I'm a little new to PHP and I'm not sure if this is used

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.