1

html タグ内のすべての情報を取得して、表に表示したいと考えています。Simple HTML DOM Parser を使用しています。次のコードを試しましたが、LAST COLUMN (Column:Total) しか取得できません。他の列からデータを取得するにはどうすればよいですか?

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 /!\ ";

上記のコードの出力は次のとおりです。

ここに画像の説明を入力

2 つの行に同じ数の列が含まれていないことに注意する必要があります。その場合、プログラムでそれを処理する必要があります。

于 2013-08-29T15:15:43.773 に答える