Simple HTML DOM Parser. Remove first row in table

2019-08-03 07:40发布

问题:

I am trying to parse an HTML string which is a table. I managed to remove the first row of the table. How can I then remove the second column in the table?

I remove the first row like this:

$preparsed = "<div style='border:1px #CCCCCC dotted; padding:5px'><div style=''>
              <div id='divPrint'>         
                  <table cellpadding='5' cellspacing='0' width='886'>
                  <tr bgcolor='#666666' class='normal' style='color:#FFFFFF; font-weight:bold;'>
                      <td width='100px' style='font-size:11px;'><b>Type</b></td>
                      <td width='110px' style='font-size:12px;'><b>Date</b></td>
                      <td width='100px' style='font-size:12px;'><b>Details</b></td>
                      <td width='140px' style='font-size:12px;'><b>Instructor</b></td>
                      <td width='140px' style='font-size:12px;'><b>Student / Client</b></td>
                      <td style='font-size:12px;'><b>Comment</b></td>
                  </tr>
                  <tr class='normal' style='font-size:11px' bgcolor='#EEEEEE'>
                    <td style='font-size:12px;'>Training</td>
                    <td style='font-size:12px;'>2016-10-05 <br /><i class='small'>(16:00:00-18:00:00)</i></td>
                    <td style='font-size:12px;'>Zara</td>
                    <td style='font-size:12px;'>Gary</td>
                    <td style='font-size:12px;'>Alfred</td>
                    <td style='font-size:12px;'></td>
                   </tr><tr class='normal' style='font-size:11px' bgcolor='#FFFFFF'>
                    <td style='font-size:12px;'>Training</td>
                    <td style='font-size:12px;'>2016-10-05 <br /><i class='small'>(12:00:00-15:00:00)</i></td>
                    <td style='font-size:12px;'>Zara</td>
                    <td style='font-size:12px;'>Gary</td>
                    <td style='font-size:12px;'>shawn</td>
                    <td style='font-size:12px;'></td>
                   </tr>
                  </table>
              </div>
              </div></div>";

$html = new simple_html_dom();
$html->load($preparsed);
$rows = array_slice($html->find('tr'), 1);
foreach ( $rows as $element ) {
echo '<h3>'. $element->plaintext . '</h3>';

This removes the first row. How can I either a)First remove every second column in each row (the date), or b) remove the first row first and then remove all the second columns in each row?

回答1:

Like this:

$html = new simple_html_dom();
$html->load($preparsed);
$rows = array_slice($html->find('tr'), 1);
foreach ( $rows as $element ) {
    echo '<h3>'. $element->plaintext . '</h3>';
    $cols = array_slice($element->find('td'), 2);
    foreach ( $cols as $col ) {
        echo '<h4>The second col os the row values '. $col->plaintext . '</h4>';
    }
}