user2631534 user2631534 - 1 month ago 10
PHP Question

How to get all td values and put in array

I have one table that I need to read td values and put in array so that I then can write to database.

Table looks like this:

<table class="maintable" width="100%">
<tbody>
<tr>
<th width="100px">Client</th>
<th width="70px">version</th>
<th width="120px">ip</th>
<th width="110px">Connected</th>
<th width="60px">TotalEcm</th>
<th width="90px">AcceptedEcm</th>
<th width="90px">EcmOK</th>
<th width="50px">EcmTime</th>
<th>Last used share</th>
</tr>
<tr>
<td class="alt3" colspan="9"> CCcam 1 (338)</td>
</tr>
<tr id="Row1" class="alt1" onmouseover="setupdateRow(1)" onmouseout="setupdateRow(0)">
<td>
<a href="/cccamclient?id=1">CLIENTNAME#1</a>
</td>
<td>CCcam 2.0.11<br>419a6380d63b8aec</td>
<td><img src="/flag_USA.gif" title="United States"> 63.15.11.121</td>
<td class="online">00d 00:08:16<table class="connect_data">
<tbody>
<tr>
<td>Successful Login: 1</td>
<td>Aborted Connections: 0</td>
<td>Total Zapping: 0</td>
<td>Channel Freeze: 0</td>
</tr>
</tbody>
</table>
</td>
<td align="center">0</td>
<td>
<span style="float: right;">0%</span>
</td>
<td><span style="float: right;">0%</span></td>
<td align="center">-- ms</td>
<td> <span style="float:right;">
<img title="disable" src="disable.png" onclick="imgrequest('/cccamclient?action=disable&amp;id=1',this);">
<img title="Debug" src="debug.png" onclick="imgrequest('/cccamclient?action=debug&amp;id=1',this);"></span>
</td>
</tr>
<tr id="Row2" class="alt2" onmouseover="setupdateRow(2)" onmouseout="setupdateRow(3)">
<td>
<a href="/cccamclient?id=2">CLIENTNAME#2</a>
</td>
<td>CCcam 2.0.11<br>419a6380d63b8aec</td>
<td><img src="/flag_AT.gif" title="Austria"> 69.75.11.121</td>
<td class="online">00d 00:08:16<table class="connect_data">
<tbody>
<tr>
<td>Successful Login: 1</td>
<td>Aborted Connections: 0</td>
<td>Total Zapping: 0</td>
<td>Channel Freeze: 0</td>
</tr>
</tbody>
</table>
</td>
<td align="center">0</td>
<td>
<span style="float: right;">0%</span>
</td>
<td><span style="float: right;">0%</span></td>
<td align="center">-- ms</td>
<td> <span style="float:right;">
<img title="disable" src="disable.png" onclick="imgrequest('/cccamclient?action=disable&amp;id=2',this);">
<img title="Debug" src="debug.png" onclick="imgrequest('/cccamclient?action=debug&amp;id=1',this);"></span>
</td>
</tr>
</tbody>




So I need when echo from array this:

Array
(
[1] => Array
(
[0] => CLIENTNAME#1
[1] => CCcam 2.0.11
[2] => 419a6380d63b8aec
[3] => /flag_USA.gif
[4] => United States
[5] -> 63.15.11.121
[6] -> 00d 00:08:16
[7] -> Successful Login: 1
[8] -> Aborted Connections: 0
[9] -> Total Zapping: 0
[10]-> Channel Freeze: 0
[11]->
)
[2] => Array
(
[0] => CLIENTNAME#2
[1] => CCcam 2.0.11
[2] => 419a6380d63b8aec
[3] => /flag_AT.gif
[4] => Austria
[5] -> 69.75.11.121
[6] -> 00d 00:08:16
[7] -> Successful Login: 1
[8] -> Aborted Connections: 0
[9] -> Total Zapping: 0
[10]-> Channel Freeze: 0
[11]->
)
)


How to get desired results? I tried with
$dom = new domDocument;
but I could not get what I need from table into array. If some one could give simple code on jsfiddle.

Answer
$dom = new domDocument;
@$dom->loadHTML(str_replace('<br>', urlencode('<br>'), $html));
$xpath = new DOMXPath($dom);

Untill here your code is true :) then correcting

// set condition that attribute id is present 
//else some first tr will be received too
$rows = $xpath->query('//tr[@id]'); 

$array_multics = array();
foreach ($rows as $row) {
  $tmp = array();
// select what you want. If I missed something, add to xpath
  foreach ( $xpath->query('.//td/text() | .//td/a/text() | .//td/img/@src', $row) as $col)  {
    $value = trim($col->nodeValue);
//exclude `-- ms` and many empty values
    if(!empty($value) && (strpos($value, '-- ms') === false))
// split <br> which comes from dom as `%3Cbr%3`
      foreach (explode('%3Cbr%3', $value) as $val) 
        $tmp[] = $val;  // here all you want
  }
  $array_multics[] = $tmp;
}

Array will be indexed by number as in the question.

Comments