parsing - PHP XPath Table elements disapearing -
i have learned xpath , wanting read data columns in table.
my current code looks this:
<?php $file_contents = file_get_contents('test.html'); $dom_document = new domdocument(); $dom_document->loadhtml($file_contents); //use domxpath navigate html dom $dom_xpath = new domxpath($dom_document); $elements = $dom_xpath->query("//tr[@class='rowstyle']"); if (!is_null($elements)) { foreach ($elements $element) { echo $element->nodevalue . '<br />'; } } else { echo 'none'; } ?>
also variation in query because through research have seen lots of issues nest table elements produces same result:
$elements = $dom_xpath->query("//table[@class='tablestyle']/tbody/tr[@class='rowstyle']");
it grab row of data makes single string, combining of cells 1 string , making tags disappear.
what want separate cells , grab row number.
i curious on how find out version of xpath have... php version 5.3.5
its not combining cells... youre outputting nodevalue
in case behaving innerhtml
. if want work on cells either use childnodes
or xpah query using row context, loop on cells.
example:
$dom_xpath = new domxpath($dom_document); $elements = $dom_xpath->query("//tr[@class='rowstyle']"); foreach ($elements $element) { foreach($element->childnodes $cell) { echo $cell->nodevalue . '<br />'; } }
Comments
Post a Comment