Jump to content

Help with PHP DOM Object(s)

Go to solution Solved by Mace,

Recommended Posts

I am only a few months into PHP and don't grasp all the things that i guess the "expert" programmers do, so for that i'm sorry... i'm working on it.

I need help with what code would accomplish what i need below, I have had only partial success but am now stuck.


I have used CURL to post to a remote site form and return the results, the returned results is the full raw HTML of that forms results page. There is only 1

table of data i need from the entire page and the only unique identifier of the table is a unique CLASS attribute. I have been able, through trial and error

to get the initial stage of assigning the HTML response to a PHP DOM object and identify the needed table by its class assignment using an example found on

the net. But: 1.) I really am having a hard time understand the whole DOM object models and manipulation. 2.) The code below, returns the TABLE i need, but all

HTML is stripped form it, thus i just get one long running line of text.  I need that table and its content, html tags and all assigned to a variable. Please help.




^^^^^^ Lots of misc HTML code above ^^^^^^

 <table class="magicname" height=100 width=100>


       <td> <p>whatever content is in here </p> </td>



       <td> <p>whatever content is in here </p> </td>



vvvvv Lots of misc HTML code below vvvv



My Current code:


$classname = 'maintbl';
$dom = new DOMDocument;
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[@class=" . $classname . "]");
foreach($results as $node) {
  echo "{$node->nodeName} - {$node->nodeValue}<br>";
  // or you can just print out the the XML:
  // $dom->saveXML($node);

Link to comment
Share on other sites

  • Solution

I have been looking for some good libraries a couple of months ago and I found the following lib:



It has very easy documantion and it really does everything you want to achive.

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images 
foreach($html->find('.maintbl td') as $element)
       echo $element->src . '<br>';

See if maybe this lib helps

Link to comment
Share on other sites

@Mace - Thank you!  It still gave me some trouble as i was trying to figure out what types of elements/attributes could be designated in the 'FIND', but your example was the key and now its working great. - Great find and thanks for sharing!

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.