简体   繁体   中英

Get data inside html tags using Simple HTML DOM Parser:

I want to get all the information inside the html tags and display them in a table. I'm using Simple HTML DOM Parser. I tried the following code but I'm only getting the LAST COLUMN (Column:Total). How do I get the data from the other columns?

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

Here's my code for the table

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

Why do you have $div->find('*') ? you can try $div->find('td') instead. This should produce correct result. Otherwise you can also try to iterate over children: foreach($div->children as $node)

Assuming you are trying to use the first row as $key and the rest for the data, you might want to alter your HTML code by simply add th in the first row, which is your header: <tr><th>…</th></tr> . This way you can get the keys by $div->find('th') . I suppose using the first row is okay as well.

As alamin.ahmed said, it would be better to search for td instead...

Here's a working example :

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

here's the output of the above code:

在此处输入图片说明

You must be aware that the two rows doesnt contain the same number of columns... then you must handle that in your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM