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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:15:00+00:00 2026-06-06T00:15:00+00:00

I tried using the following code for a HTML page, but it doesn’t work.

  • 0

I tried using the following code for a HTML page, but it doesn’t work. How do I retrieve and manipulate all outputted HTML elements in one page?

$doc = new DOMDocument;
$doc->load('http://localhost/foo/index.php');

$items = $doc->getElementsByTagName('img');

foreach ($items as $item) {
    echo $item->nodeValue . "\n";
}

EDIT:

$dom = new DOMDocument;
$html = 'http://localhost/foo/index.php';
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node) {
    echo $dom->saveHtml($node), PHP_EOL;

}

The code above outputs nothing

Debugging Code:

<?php

$dom = new DOMDocument;
$html = 'http://localhost/foo/index.php';

var_dump($dom->loadHTML($html));

echo '<br />';

var_dump($dom);

echo '<br />';

var_dump($dom->saveHTML());

echo '<br />';

var_dump($dom->getElementsByTagName('a'));

echo '<br />';

foreach ($dom->getElementsByTagName('a') as $node) {

    var_dump($node);

    echo '<br />';

    var_dump( $dom->saveHtml($node) );
    echo '<br />';

}

?>

Debugging Result:

bool(true)
object(DOMDocument)#1 (0) { }
string(170) "

http://localhost/foo/index.php
"
object(DOMNodeList)#2 (0) { } 
  • 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-06T00:15:01+00:00Added an answer on June 6, 2026 at 12:15 am

    Some DOMDocument debugging hints.

    If applicable upgrade to the latest PHP 5.4 because it will give you more information on var_dump for DOMDocument and friends.

    I take your small example and will add some hints how to debug the code:

    $dom = new DOMDocument;
    $html = 'http://localhost/foo/index.php';
    $dom->loadHTML($html);
    foreach ($dom->getElementsByTagName('a') as $node) {
        echo $dom->saveHtml($node), PHP_EOL;
    }
    

    Did the loading work? That is this line:

    $dom->loadHTML($html);
    

    You can take a look inside the document by outputting it’s content. If you display that in the browser you need to look into the source of your document or you just change the output with htmlspecialchars:

    var_dump(htmlspecialchars($dom->saveHTML()));
    

    This will give you the documented as loaded in the HTML variant verbatim inside your browser.

    The next part you might want to debug is the result of getElementsByTagName:

    foreach ($dom->getElementsByTagName('a') as $node) {
    

    First assign it to a variable, and then check the length if it’s not NULL or FALSE:

    $aTags = $dom->getElementsByTagName('a');
    var_dump(htmlspecialchars($aTags), $aTags->length());
    

    The length will tell you how many elements were matched.

    Example/Demo:

    <?php
    
    $dom = new DOMDocument;
    $html = 'http://localhost/foo/index.php';
    $dom->loadHTML($html);
    echo 'Document HTML loaded: ', var_dump($dom->saveHTML()), "\n";
    $aTags = $dom->getElementsByTagName('a');
    echo 'A Elements found: ', var_dump($aTags->length), "\n";
    foreach ($aTags as $node) {
       echo $dom->saveHtml($node), "\n";
    }
    

    Output:

    Document HTML loaded: string(171) "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html><body><p>http://localhost/foo/index.php</p></body></html>
    "
    
    A Elements found: int(0)
    

    Hope this is helpful.

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

Sidebar

Related Questions

I was using the following code to retrieve HTML snippets from a database table
I tried using the following code in .java(main activity): final ImageView diskView1 = (ImageView)
I tried to modify a type using the following code and it gave me
I tried to achieve the following using Twitter Bootstrap 2.0 and it's popover-plugin, but
I have a login page using jQuery Mobile which contains the following code: <div
I tried to serve an html page that includes unicode text using django and
I tried the following code to send request to jsp page on a click
I have a html-page which I would like to style (using IE9). The following
I've tried to change filenames using following script: find dir/ -type f -exec mv
I tried to reuse the .pch to speed the build using the following way:

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.