0

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.

1
  • You can get the ChildNodes and get the values from those Commented Feb 7, 2017 at 3:58

1 Answer 1

3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.