twittoris Posted September 8, 2010 Share Posted September 8, 2010 What would be the best way to break the following html into a table <div class="highlight">Name: CHRISTOPHER</div> <table summary="This table contains status information for the selected name."> <caption>Selected name status Information</caption> <tr> <th>Current Name:</th> <td>CHRISTOPHER</td> </tr> <tr> <th>Initial Filing Date:</th> <td>DECEMBER 15, 1997</td> </tr> <tr> <th>County:</th> <td>NEW YORK</td> </tr> <tr> <th>Jurisdiction:</th> <td>NEW YORK</td> </tr> <tr> <th>Type:</th> <td>Full Member</td> </tr> Would pregmatch or COMDocument be easier? Quote Link to comment https://forums.phpfreaks.com/topic/212819-whats-the-best-way-to-place-this-into-mysql/ Share on other sites More sharing options...
joel24 Posted September 8, 2010 Share Posted September 8, 2010 What would be the best way to break the following html into a table it's already a table... hah, you mean individual values so you can place them in a database table? Quote Link to comment https://forums.phpfreaks.com/topic/212819-whats-the-best-way-to-place-this-into-mysql/#findComment-1108513 Share on other sites More sharing options...
twittoris Posted September 8, 2010 Author Share Posted September 8, 2010 yes exactly. Quote Link to comment https://forums.phpfreaks.com/topic/212819-whats-the-best-way-to-place-this-into-mysql/#findComment-1108514 Share on other sites More sharing options...
twittoris Posted September 8, 2010 Author Share Posted September 8, 2010 I want to place each piece into a table within mysql database. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/212819-whats-the-best-way-to-place-this-into-mysql/#findComment-1108520 Share on other sites More sharing options...
objnoob Posted September 8, 2010 Share Posted September 8, 2010 Is your goal is to extract the data pieces from the HTML, which you could then use to build an SQL Insert statement from? Quote Link to comment https://forums.phpfreaks.com/topic/212819-whats-the-best-way-to-place-this-into-mysql/#findComment-1108521 Share on other sites More sharing options...
twittoris Posted September 8, 2010 Author Share Posted September 8, 2010 yes exactly Quote Link to comment https://forums.phpfreaks.com/topic/212819-whats-the-best-way-to-place-this-into-mysql/#findComment-1108523 Share on other sites More sharing options...
objnoob Posted September 8, 2010 Share Posted September 8, 2010 I would instantiate a DOMDocument object, and use the getElementsByTagName to gather the <TH> and <TD> tags. With arrays of the TH and TD tags, I would use a for loop through the TH tags, using a switch on innerHTML with cases for each label (ex: Current Name:) and grab the corresponding <TD> tags innerHTML. This is a basic solution for the HTML you've provided me, given there are an equal number of TH to TD tags. Once the HTML includes other tables without a 1:1 relational ratio this solution suffers model breakdown. Quote Link to comment https://forums.phpfreaks.com/topic/212819-whats-the-best-way-to-place-this-into-mysql/#findComment-1108527 Share on other sites More sharing options...
twittoris Posted September 8, 2010 Author Share Posted September 8, 2010 I tried to do it this way: // make the cURL request to $target_url $html2 = curl_exec($ch2); // parse the html into a DOMDocument $dom2 = new DOMDocument(); @$dom2->loadHTML($html2); /*** discard white space ***/ $dom2->preserveWhiteSpace = false; /*** the table by its tag name ***/ $tables = $dom2->getElementsByTagName('table'); /*** get all rows from the table ***/ $rows2 = $tables->item(0)->getElementsByTagName('tr'); /*** loop over the table rows ***/ foreach ($rows2 as $row2); { /*** get each column by tag name ***/ $cols = $row2->getElementsByTagName('td'); /*** echo the values ***/ echo $cols->item(0)->nodeValue.'<br />'; echo $cols->item(1)->nodeValue.'<br />'; echo $cols->item(2)->nodeValue.'<br />'; echo $cols->item(3)->nodeValue.'<br />'; echo $cols->item(4)->nodeValue; echo '<hr />'; But it isnt returning anything except the word ACTIVE which is the last table value in HTML. Quote Link to comment https://forums.phpfreaks.com/topic/212819-whats-the-best-way-to-place-this-into-mysql/#findComment-1108685 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.