title: Convert HTML table to CSV via PHP
body: <p>I am trying to pull each td element from the html table below and import each element into its own cell in a CSV file. </p>
  
  <p>Here are the two html tables:</p>
  
  <pre><code>&lt;table width="100%"  border="0" cellspacing="1" cellpadding="0" bgcolor="#006699"&gt;
  &lt;tr align="center" class="tableRow1Font"&gt;
  &lt;td width="7%"&gt;WAITLIST&lt;/td&gt;
  &lt;td width="5%"&gt;91630&lt;/td&gt;
  &lt;td width="11%"&gt;
  ACCY &lt;A HREF="http://www.gwu.edu/~bulletin/ugrad/accy.html#2001" target="_blank"&gt;2001&lt;/A&gt;
  &lt;/td&gt;
  &lt;td width="5%"&gt;10&lt;/td&gt;
  &lt;td width="16%"&gt;Intro Financial Accounting&lt;/td&gt;
  &lt;td width="6%"&gt;3.00&lt;/td&gt;
  &lt;td width="8%"&gt; Zou, Y&lt;/td&gt;
  &lt;td width="8%"&gt;&lt;A HREF="http://www.gwu.edu/~map/building.cfm?BLDG=DUQUES" target="_blank"
  &gt;DUQUES&lt;/a&gt; 251&lt;/td&gt;
  &lt;td width="13%"&gt;TR&lt;br&gt;09:35AM - 10:50AM&lt;/td&gt;
  &lt;td width="14%"&gt;
  01/13/14 - 04/28/14
  &lt;/td&gt;
  &lt;td width="7%"&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;table width="100%"  border="0" cellspacing="1" cellpadding="0" bgcolor="#006699"&gt;
  &lt;tr align="center" class="tableRow2Font"&gt;
  &lt;td width="7%"&gt;WAITLIST&lt;/td&gt;
  &lt;td width="5%"&gt;90003&lt;/td&gt;
  &lt;td width="11%"&gt;
  ACCY &lt;A HREF="http://www.gwu.edu/~bulletin/ugrad/accy.html#2001" target="_blank"&gt;2001&lt;/A&gt;
  &lt;/td&gt;
  &lt;td width="5%"&gt;11&lt;/td&gt;
  &lt;td width="16%"&gt;Intro Financial Accounting&lt;/td&gt;
  &lt;td width="6%"&gt;3.00&lt;/td&gt;
  &lt;td width="8%"&gt; Zou, Y&lt;/td&gt;
  &lt;td width="8%"&gt;&lt;A HREF="http://www.gwu.edu/~map/building.cfm?BLDG=DUQUES" target="_blank" 
  &gt;DUQUES&lt;/a&gt; 254&lt;/td&gt;
  &lt;td width="13%"&gt;TR&lt;br&gt;11:10AM - 12:25PM&lt;/td&gt;
  &lt;td width="14%"&gt;
  1/13/14 - 04/28/14
  &lt;/td&gt;
  &lt;td width="7%"&gt;                                     
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I have written code that goes through the tables and pulls the td elements:</p>
  
  <pre><code>    foreach($html-&gt;find('tr[align=center] td') as $e)
      $str .= strip_tags($e-&gt;innertext) . ', ';
      echo $str;
  </code></pre>
  
  <p>So how can I extract these elements into a CSV file? In Excel I want it to look like this with each td element in its own cell, starting a new row for each html table:</p>
  
  <pre><code>WAITLIST  91630  ACCY 2001  10  Intro Financial Accounting  3.00  Zou, Y  DUQUES 251  TR  
  WAITLIST  90003  ACCY 2001  11  Intro Financial Accounting  3.00  Zou, Y  DUQUES 251  TR
  </code></pre>