title: Convert HTML to CSV in php?
body: <p>I have a html table structure like this;</p>
  
  <pre><code>            &lt;tr style="font-weight: bold"&gt;
                  &lt;td&gt;ID&lt;/td&gt;
                  &lt;td&gt;Navn&lt;/td&gt;
  
                  &lt;td&gt;Adresse&lt;/td&gt;
                  &lt;td&gt;By&lt;/td&gt;
                  &lt;td&gt;Post nr&lt;/td&gt;
                  &lt;td&gt;E-mail&lt;/td&gt;
                  &lt;td&gt;Telefon&lt;/td&gt;
                  &lt;td&gt;Status og dato&lt;/td&gt;
                  &lt;td&gt;Dropdown info&lt;/td&gt;
                  &lt;td&gt;Produkt info&lt;/td&gt;
                  &lt;td&gt;Buydate&lt;/td&gt;
                  &lt;td&gt;Ref nr. (3 første cifre)&lt;/td&gt;
              &lt;/tr&gt;
                      &lt;tr&gt;
                  &lt;td&gt;40563&lt;/td&gt;
                  &lt;td&gt;Firstname Lastname&lt;/td&gt;
  
                  &lt;td&gt;Address&lt;/td&gt;
                  &lt;td&gt;Copen&lt;/td&gt;
                  &lt;td&gt;2100&lt;/td&gt;
                  &lt;td&gt;ff@hotmail.com&lt;/td&gt;
                  &lt;td&gt;123123&lt;/td&gt;
                  &lt;td&gt;Ikke indløst&lt;/td&gt;
                  &lt;td&gt;EEE-BBB&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>I would like to convert this into a csv/excel file by php.</p>
  
  <p>So each  is a row in excel, and each  is a cell in the row,</p>
  
  <p>Please how can this be done?</p>
  
  <p>I have researched and found <a href="https://stackoverflow.com/questions/10498632/converting-html-table-to-a-csv-automatically-using-php">Converting HTML Table to a CSV automatically using PHP?</a> but the answer does not work properly for me, Im getting all the cell results in one 'cell', so each row only have one cell.</p>
  
  <p>This is what i have tried;</p>
  
  <pre><code>        $html = str_get_html($table);
  
  
  
          header('Content-type: application/ms-excel');
          header('Content-Disposition: attachment; filename=sample.csv');
  
          $fp = fopen("php://output", "w");
  
          foreach($html-&gt;find('tr') as $element)
          {
              $td = array();
              foreach( $element-&gt;find('td') as $row)  
              {
                  $td [] = $row-&gt;plaintext;
              }
              fputcsv($fp, $td);
          }
  
  
          fclose($fp);
          exit;
  </code></pre>
  
  <p>Where $table is the html above. Using simple html dom plugin</p>