dmsinc Posted January 22, 2014 Share Posted January 22, 2014 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); Quote Link to comment https://forums.phpfreaks.com/topic/285588-help-with-php-dom-objects/ Share on other sites More sharing options...
Solution Mace Posted January 22, 2014 Solution Share Posted January 22, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/285588-help-with-php-dom-objects/#findComment-1466183 Share on other sites More sharing options...
dmsinc Posted January 22, 2014 Author Share Posted January 22, 2014 @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! Quote Link to comment https://forums.phpfreaks.com/topic/285588-help-with-php-dom-objects/#findComment-1466202 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.