1

我想获取 html 标签内的所有信息并将它们显示在表格中。我正在使用简单的 HTML DOM 解析器。我尝试了以下代码,但我只得到最后一列(列:总计)。如何从其他列中获取数据?

foreach($html->find('tr[class="tblRowShade"]') as $div) {
    $key = '';
    $val = '';

    foreach($div->find('*') as $node) {
        if ($node->tag=='td'){
            $key = $node->plaintext;
        }
    }

    $ret[$key] = $val;
}

这是我的表格代码

 <tr class="tblRowShade">
      <td width="12%"><strong>Project</strong></td>
      <td width="38%">&nbsp;</td>
      <td width="25%"><strong>Recipient</strong></td>
      <td width="14%"><strong>Municipality/City</strong></td>
      <td width="11%" nowrap="nowrap" class="td_right"><strong>Implementing Unit</strong></td>
      <td width="11%" nowrap="nowrap" class="td_right"><strong>Release Date</strong></td>
      <td align="right" width="11%" class="td_right"><strong>Total</strong></td>
 </tr>

<tr class="tblRowShade">
      <td colspan="2" >Livelihood Programs</td>
      <td >Basic Espresso and Latte</td>
      <td nowrap="nowrap"></td>
      <td >DOLE - TESDA Regional Office IV-A</td>
      <td nowrap="nowrap">2013-06-11</td>
      <td align="right" nowrap="nowrap" class="td_right">1,500,000</td>
</tr>
4

2 回答 2

0

你为什么有$div->find('*')?你可以试试$div->find('td')。这应该会产生正确的结果。否则,您也可以尝试迭代孩子:foreach($div->children as $node)

假设您尝试将第一行用作 $key 并将其余行用作数据,您可能希望通过简单地添加th第一行来更改您的 HTML 代码,即您的标题:<tr><th>…&lt;/th></tr>。这样你就可以通过$div->find('th'). 我想使用第一行也可以。

于 2013-08-29T12:30:15.480 回答
0

正如alamin.ahmed所说,最好搜索td...

这是一个工作示例:

$text = ' <tr class="tblRowShade">
      <td width="12%"><strong>Project</strong></td>
      <td width="38%">&nbsp;</td>
      <td width="25%"><strong>Recipient</strong></td>
      <td width="14%"><strong>Municipality/City</strong></td>
      <td width="11%" nowrap="nowrap" class="td_right"><strong>Implementing Unit</strong></td>
      <td width="11%" nowrap="nowrap" class="td_right"><strong>Release Date</strong></td>
      <td align="right" width="11%" class="td_right"><strong>Total</strong></td>
 </tr>

<tr class="tblRowShade">
      <td colspan="2" >Livelihood Programs</td>
      <td >Basic Espresso and Latte</td>
      <td nowrap="nowrap"></td>
      <td >DOLE - TESDA Regional Office IV-A</td>
      <td nowrap="nowrap">2013-06-11</td>
      <td align="right" nowrap="nowrap" class="td_right">1,500,000</td>
</tr>';

echo  "<div>Original Text: <xmp>$text</xmp></div>";


//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($text);


// Find all elements
$rows = $html->find('tr[class="tblRowShade"]');


// Find succeeded
if ($rows) {

    echo count($rows) . " \$rows found !<br />";

    foreach ($rows as $key => $row) {

        echo "<hr />";

        $columns = $row->find('td');

        // Find succeeded
        if ($rows) {

            echo count($columns) . " \$columns found  in \$rows[$key]!<br />";

            foreach ($columns as $col) {

                    echo $col->plaintext . " | ";
                }
        }
        else
            echo " /!\ Find() \$columns failed /!\ ";
    }
}
else
    echo " /!\ Find() \$rows failed /!\ ";

这是上面代码的输出:

在此处输入图像描述

您必须知道这两行不包含相同数量的列...然后您必须在程序中处理它。

于 2013-08-29T15:15:43.773 回答