Jump to content

Help with PHP DOM Object(s)


dmsinc

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.

 

Example:

 

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

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

    <tr>

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

   </tr>

    <tr>

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

   </tr>

 </table>

vvvvv Lots of misc HTML code below vvvv

 

 

My Current code:

 

$classname = 'maintbl';
$dom = new DOMDocument;
$dom->loadHTML($server_output);
$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
https://forums.phpfreaks.com/topic/285588-help-with-php-dom-objects/
Share on other sites

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

http://simplehtmldom.sourceforge.net/

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.