b85411 b85411 - 4 years ago 201
PHP Question

PHP XPath to parse table

Firstly here is my table HTML:

<table class="xyz">
<caption>Outcomes</caption>
<thead>
<tr class="head">
<th title="a" class="left" nowrap="nowrap">A1</th>
<th title="a" class="left" nowrap="nowrap">A2</th>
<th title="result" class="left" nowrap="nowrap">Result</th>
<th title="margin" class="left" nowrap="nowrap">Margin</th>
<th title="area" class="left" nowrap="nowrap">Area</th>
<th title="date" nowrap="nowrap">Date</th>
<th title="link" nowrap="nowrap">Link</th>
</tr>
</thead>
<tbody>
<tr class="data1">
<td class="left" nowrap="nowrap">56546</td>
<td class="left" nowrap="nowrap">75666</td>
<td class="left" nowrap="nowrap">Lower</td>
<td class="left" nowrap="nowrap">High</td>
<td class="left">Area 3</td>
<td nowrap="nowrap">Jan 2 2016</td>
<td nowrap="nowrap">http://localhost/545436</td>
</tr>
<tr class="data1">
<td class="left" nowrap="nowrap">55546</td>
<td class="left" nowrap="nowrap">71666</td>
<td class="left" nowrap="nowrap">Lower</td>
<td class="left" nowrap="nowrap">High</td>
<td class="left">Area 4</td>
<td nowrap="nowrap">Jan 3 2016</td>
<td nowrap="nowrap">http://localhost/545437</td>
</tr>
...


And there are many more
<tr>
after that.

I am using this PHP code:

$html = file_get_contents('http://localhost/outcomes');

$document = new DOMDocument();
$document->loadHTML($html);

$xpath = new DOMXPath($document);
$xpath->registerNamespace('', 'http://www.w3.org/1999/xhtml');
$elements = $xpath->query("//table[@class='xyz']");


How can I, now that I have the table as the first element in
$elements
, get the values of each
<td>
?

Ideally I want to get arrays like:

array(56546, 75666, 'Lower', 'High', 'Area 3', 'Jan 2 2016', 'http://localhost/545436'),
array(55546, 71666, 'Lower', 'High', 'Area 4', 'Jan 3 2016', 'http://localhost/545437'),
...


But I'm not sure how I can dig that deeply into the the table code.

Thank you for any advice.

Answer Source

First, get all the table rows in the <tbody>

$rows = $xpath->query('//table[@class="xyz"]/tbody/tr');

Then, you can iterate over that collection and query for each <td>

foreach ($rows as $row) {
    $cells = $row->getElementsByTagName('td');
    // alt $cells = $xpath->query('td', $row)

    $cellData = [];
    foreach ($cells as $cell) {
        $cellData[] = $cell->nodeValue;
    }
    var_dump($cellData);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download