如何获取所有td值并放入数组

时间:2015-05-31 07:27:56

标签: php arrays html-table

我有一个表,我需要读取td值并放入数组,以便我可以写入数据库。

表格如下:

<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>

所以我需要来自数组的回声:

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]->
        )
)

如何获得理想的结果?我尝试使用$dom = new domDocument;,但我无法从表格到数组获得所需内容。如果有人可以在jsfiddle上给出简单的代码。

2 个答案:

答案 0 :(得分:1)

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

直到这里你的代码是真的:)然后纠正

// 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;
}

数组将按问题编号索引。

答案 1 :(得分:0)

如果您查看上面评论中发布的代码示例 - http://codepad.org/uOBcv6W8 - 您的HTML字符串引用错误,问题就很明显了。

您可以使用heredoc(现在称为newdoc)语法来引用带有嵌入式引号的HTML,例如(来自https://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

<?php
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;