Charvusta Posted October 1, 2017 Share Posted October 1, 2017 I'm trying to output only the first column in this table, do I have to add another getElementsByTagName(); this is my code, <?php function tdrows($elements) { $str = null; foreach ($elements as $element) { $str .= $element->nodeValue . ", "; } return $str; } function getdata() { $contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>"; $DOM = new DOMDocument; $DOM->loadHTML($contents); $items = $DOM->getElementsByTagName('tr'); foreach ($items as $node) { echo tdrows($node->childNodes) . "<br />"; } } //tdrows(); getdata(); ?> The output is Cheers Link to comment Share on other sites More sharing options...
ginerjm Posted October 1, 2017 Share Posted October 1, 2017 I"m confused. You ask how to only show the first column, but then you show an example with two columns. Link to comment Share on other sites More sharing options...
Charvusta Posted October 1, 2017 Author Share Posted October 1, 2017 Lol, yeah, i only want to show column 1, does that make sense? Link to comment Share on other sites More sharing options...
Charvusta Posted October 1, 2017 Author Share Posted October 1, 2017 $contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>"; Is this possible? Have a little bit of regex experience from another language. Cheers Link to comment Share on other sites More sharing options...
requinix Posted October 1, 2017 Share Posted October 1, 2017 With difficulty, sure. A better approach is to load the string into DOM and locate the column with its tools. $dom = new DOMDocument(1.0, "utf-8"); $dom->loadHTML($contents); // if $contents only has the one <table> and you want the first <td> from the first/only <tr> $td = $dom->getElementsByTagName("td"); if ($td->length) { $value = $td[0]->textContent; } else { // error } Link to comment Share on other sites More sharing options...
Charvusta Posted October 1, 2017 Author Share Posted October 1, 2017 I'm playing around with this similar example i found Link to comment Share on other sites More sharing options...
requinix Posted October 1, 2017 Share Posted October 1, 2017 One thread at a time, please. Link to comment Share on other sites More sharing options...
Charvusta Posted October 2, 2017 Author Share Posted October 2, 2017 With difficulty, sure. A better approach is to load the string into DOM and locate the column with its tools. $dom = new DOMDocument(1.0, "utf-8"); $dom->loadHTML($contents); // if $contents only has the one <table> and you want the first <td> from the first/only <tr> $td = $dom->getElementsByTagName("td"); if ($td->length) { $value = $td[0]->textContent; } else { // error } Wow thats a great way using if length, thanks Link to comment Share on other sites More sharing options...
Charvusta Posted October 2, 2017 Author Share Posted October 2, 2017 This is what i done to my code to get it working <?php function getdata() { $contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>"; $DOM = new DOMDocument; $DOM->loadHTML($contents); $table = $DOM->getElementsByTagName('table'); $rows = $DOM->getElementsByTagName('tr'); foreach ($rows as $row) { $cols = $row->getElementsByTagName('td'); echo $cols->item(0)->nodeValue.'<br />'; } } getdata(); ?> output is now Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.