1<?php
2
3    //download @ http://simplehtmldom.sourceforge.net/
4    require_once('simple_html_dom.php');
5    $repeatContentIntoSpannedCells = false;
6
7
8    //--------------------------------------------------------------------------------------------------------------------
9
10    function table2csv($rawHTML,$filename,$repeatContent, $caption) {
11
12        //get rid of sups - they mess up the wmus
13        for ($i=1; $i <= 20; $i++) {
14            $rawHTML = str_replace("<sup>".$i."</sup>", "", $rawHTML);
15        }
16
17        global $repeatContentIntoSpannedCells;
18
19        $html = str_get_html(trim($rawHTML));
20        $repeatContentIntoSpannedCells = $repeatContent;
21
22        //we need to pre-initialize the array based on the size of the table (how many rows vs how many columns)
23
24        //counting rows is easy
25        $rowCount = count($html->find('tr'));
26
27        //column counting is a bit trickier, we have to iterate through the rows and basically pull out the max found
28        $colCount = 0;
29        $rowPos = 0;
30        foreach ($html->find('tr') as $element) {
31          if ($rowPos!=0 || $caption==0) {
32
33            $tempColCount = 0;
34
35            foreach ($element->find('th') as $cell) {
36                $tempColCount++;
37            }
38
39            if ($tempColCount == 0) {
40                foreach ($element->find('td') as $cell) {
41                    $tempColCount++;
42                }
43            }
44
45            if ($tempColCount > $colCount) $colCount = $tempColCount;
46          }
47        $rowPos = 1;
48        }
49
50        $mdTable = array();
51
52        for ($i=0; $i < $rowCount; $i++) {
53            if ($i!=0 || $caption==0)
54            array_push($mdTable, array_fill(0, $colCount, NULL));
55        }
56
57        //////////done predefining array
58
59        $rowPos = 0;
60        $fp = fopen($filename, "w");
61
62        foreach ($html->find('tr') as $element) {
63          if ($rowPos!=0 || $caption==0) {
64
65            $colPos = 0;
66
67            foreach ($element->find('th') as $cell) {
68                if (strpos(trim($cell->class), 'actions') === false && strpos(trim($cell->class), 'checker') === false) {
69                    parseCell($cell,$mdTable,$rowPos,$colPos);
70                }
71                $colPos++;
72            }
73
74            foreach ($element->find('td') as $cell) {
75                if (strpos(trim($cell->class), 'actions') === false && strpos(trim($cell->class), 'checker') === false) {
76                    parseCell($cell,$mdTable,$rowPos,$colPos);
77                }
78                $colPos++;
79            }
80          }
81
82          $rowPos++;
83        }
84
85
86        foreach ($mdTable as $key => $row) {
87
88            //clean the data
89            array_walk($mdTable[$key], "cleanCell");
90            fputcsv($fp, $row);
91        }
92        return $mdTable;
93    }
94
95
96    function cleanCell(&$contents,$key) {
97
98        $contents = trim($contents);
99
100        //get rid of pesky &nbsp's (aka: non-breaking spaces)
101        $contents = trim($contents,chr(0xC2).chr(0xA0));
102        $contents = str_replace("&nbsp;", "", $contents);
103    }
104
105
106    function parseCell(&$cell,&$mdTable,&$rowPos,&$colPos) {
107
108        global $repeatContentIntoSpannedCells;
109
110        //if data has already been set into the cell, skip it
111        while (isset($mdTable[$rowPos][$colPos])) {
112            $colPos++;
113        }
114
115        $mdTable[$rowPos][$colPos] = $cell->plaintext;
116
117        if (isset($cell->rowspan)) {
118
119            for ($i=1; $i <= ($cell->rowspan)-1; $i++) {
120                $mdTable[$rowPos+$i][$colPos] = ($repeatContentIntoSpannedCells ? $cell->plaintext : "");
121            }
122        }
123
124        if (isset($cell->colspan)) {
125
126            for ($i=1; $i <= ($cell->colspan)-1; $i++) {
127
128                $colPos++;
129                $mdTable[$rowPos][$colPos] = ($repeatContentIntoSpannedCells ? $cell->plaintext : "");
130            }
131        }
132    }
133
134?>