简单的HTML DOM解析器。删除表中的第一行,然后删除每一列的第二列 [英] Simple HTML DOM Parser. Remove first row in table and then remove second column of each

查看:116
本文介绍了简单的HTML DOM解析器。删除表中的第一行,然后删除每一列的第二列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个表格的HTML字符串。我设法删除了表的第一行。我如何删除表中的第二列?

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

这将删除第一行, a)首先删除每行(日期)中的每一列,或者b)首先删除第一行,然后删除每行中的所有第二列?

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?

推荐答案

像这样:

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

这篇关于简单的HTML DOM解析器。删除表中的第一行,然后删除每一列的第二列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆