1

Let's say I have the following HTML table:

<table>
     <tbody>
           <tr>
               <th>Column 1</th>
               <th>Column 2</th>
               <th>Column 3</th>
               <th>Column 4</th>
               <th>Column 5</th>
           </tr>
           <tr>
               <td>Column 1</td>
               <td>Column 2</td>
               <td>Column 3</td>
               <td>Column 4</td>
               <td>Column 5</td>
           </tr>
     </tbody>
</table>

How would I use the simple HTML DOM parser to return the table without columns 4 and 5? the first three columns of the table? I've started with this to get each row:

foreach ($html->find('tr') as $row) {

}

would I run another for loop to iterate through the cells?

D.Wells
  • 125
  • 10
  • 1
    Possible duplicate of [Retrieve data from the first td in every tr](https://stackoverflow.com/questions/14080986/retrieve-data-from-the-first-td-in-every-tr) – k0pernikus Aug 23 '17 at 10:47

1 Answers1

1

You need to remove the 4th and 5th columns of each row (if they exist, and I just assume they do). Please note children of an element are zero-indexed:

foreach ($html->find('tr') as $row) {
    // remove the 4th column
    $row->children(3)->outertext = '';
    // remove the 5th column
    $row->children(4)->outertext = '';
}

print $html; // Outputs the table without 4th and 5th column of each row;

You can read a related answer here Simple HTML Dom: How to remove elements?

Nima
  • 3,309
  • 6
  • 27
  • 44