Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've adapted a simple class based on the code found on this thread that now handles <code>colspan</code> and <code>rowspan</code>. Not heavily tested and I'm sure it could be optimized.</p> <p>Usage:</p> <pre><code>require_once('table2csv.php'); $table = '&lt;table border="1"&gt; &lt;tr&gt; &lt;th colspan=2&gt;Header 1&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;row 1, cell 1&lt;/td&gt; &lt;td&gt;row 1, cell 2&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;row 2, cell 1&lt;/td&gt; &lt;td&gt;row 2, cell 2&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td rowspan=2&gt;top left row&lt;/td&gt; &lt;td&gt;top right row&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;bottom right&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;'; table2csv($table,"sample.csv",true); </code></pre> <p>table2csv.php</p> <pre><code>&lt;?php //download @ http://simplehtmldom.sourceforge.net/ require_once('simple_html_dom.php'); $repeatContentIntoSpannedCells = false; //-------------------------------------------------------------------------------------------------------------------- function table2csv($rawHTML,$filename,$repeatContent) { //get rid of sups - they mess up the wmus for ($i=1; $i &lt;= 20; $i++) { $rawHTML = str_replace("&lt;sup&gt;".$i."&lt;/sup&gt;", "", $rawHTML); } global $repeatContentIntoSpannedCells; $html = str_get_html(trim($rawHTML)); $repeatContentIntoSpannedCells = $repeatContent; //we need to pre-initialize the array based on the size of the table (how many rows vs how many columns) //counting rows is easy $rowCount = count($html-&gt;find('tr')); //column counting is a bit trickier, we have to iterate through the rows and basically pull out the max found $colCount = 0; foreach ($html-&gt;find('tr') as $element) { $tempColCount = 0; foreach ($element-&gt;find('th') as $cell) { $tempColCount++; } if ($tempColCount == 0) { foreach ($element-&gt;find('td') as $cell) { $tempColCount++; } } if ($tempColCount &gt; $colCount) $colCount = $tempColCount; } $mdTable = array(); for ($i=0; $i &lt; $rowCount; $i++) { array_push($mdTable, array_fill(0, $colCount, NULL)); } //////////done predefining array $rowPos = 0; $fp = fopen($filename, "w"); foreach ($html-&gt;find('tr') as $element) { $colPos = 0; foreach ($element-&gt;find('th') as $cell) { if (strpos(trim($cell-&gt;class), 'actions') === false &amp;&amp; strpos(trim($cell-&gt;class), 'checker') === false) { parseCell($cell,$mdTable,$rowPos,$colPos); } $colPos++; } foreach ($element-&gt;find('td') as $cell) { if (strpos(trim($cell-&gt;class), 'actions') === false &amp;&amp; strpos(trim($cell-&gt;class), 'checker') === false) { parseCell($cell,$mdTable,$rowPos,$colPos); } $colPos++; } $rowPos++; } foreach ($mdTable as $key =&gt; $row) { //clean the data array_walk($row, "cleanCell"); fputcsv($fp, $row); } } function cleanCell(&amp;$contents,$key) { $contents = trim($contents); //get rid of pesky &amp;nbsp's (aka: non-breaking spaces) $contents = trim($contents,chr(0xC2).chr(0xA0)); $contents = str_replace("&amp;nbsp;", "", $contents); } function parseCell(&amp;$cell,&amp;$mdTable,&amp;$rowPos,&amp;$colPos) { global $repeatContentIntoSpannedCells; //if data has already been set into the cell, skip it while (isset($mdTable[$rowPos][$colPos])) { $colPos++; } $mdTable[$rowPos][$colPos] = $cell-&gt;plaintext; if (isset($cell-&gt;rowspan)) { for ($i=1; $i &lt;= ($cell-&gt;rowspan)-1; $i++) { $mdTable[$rowPos+$i][$colPos] = ($repeatContentIntoSpannedCells ? $cell-&gt;plaintext : ""); } } if (isset($cell-&gt;colspan)) { for ($i=1; $i &lt;= ($cell-&gt;colspan)-1; $i++) { $colPos++; $mdTable[$rowPos][$colPos] = ($repeatContentIntoSpannedCells ? $cell-&gt;plaintext : ""); } } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload