-
Notifications
You must be signed in to change notification settings - Fork 0
/
html2cell.php
135 lines (85 loc) · 2.58 KB
/
html2cell.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
#html2cell is a function designed to parse html into arrays;
function html2cell($html)
{
#parse what's inside the table
if (eregi('<TABLE>(.*)</TABLE>', $html, $table_contents)) {
#parse teh lines
if (eregi('<TR>(.*)</TR>',$table_contents[1], $row_contents)) {
#$row = explode('<TR>', $row_contents[0]);
$row = spliti('<TR>', $row_contents[0]);
$row = array_filter($row);#explode has this annoying habit of adding empty values
foreach ($row as $rowi=>$a_row) {
#remove </tr>
$a_row = str_ireplace('</TR>', '', $a_row);
#parse teh cells
if (eregi('<TD>(.*)</TD>', $a_row, $cell_contents)) {
$cells[$rowi] = spliti('<TD>', $cell_contents[1]);
#remove emptyes
$cells[$rowi] = array_filter($cells[$rowi]);
#remove the /td
$cells[$rowi] = array_filter($cells[$rowi]);
foreach ($cells[$rowi] as $col=>$a_cell) {
$a_cell = str_ireplace('</TD>', '', $a_cell);
$cells[$rowi][$col] = $a_cell;
if ($rowi>1) {
$cells[$rowi][trim($cells[1][$col])] = $a_cell;
}
}
}
else {
return ('No cells');
}
}
}
else {
return ('No rows');
}
}
else {
'No html tables found';
}
return ($cells);
}
function html2array($html)
{
#parse what's inside the table
if (eregi('<TABLE>(.*)</TABLE>', $html, $table_contents)) {
#parse teh lines
if (eregi('<TR>(.*)</TR>',$table_contents[1], $row_contents)) {
#$row = explode('<TR>', $row_contents[0]);
$row = spliti('<TR>', $row_contents[0]);
$row = array_filter($row);#explode has this annoying habit of adding empty values
foreach ($row as $rowi=>$a_row) {
#remove </tr>
$a_row = str_ireplace('</TR>', '', $a_row);
#parse teh cells
if (eregi('<TD>(.*)</TD>', $a_row, $cell_contents)) {
$cells[$rowi] = spliti('<TD>', $cell_contents[1]);
#remove emptyes
$cells[$rowi] = array_filter($cells[$rowi]);
#remove the /td
$cells[$rowi] = array_filter($cells[$rowi]);
foreach ($cells[$rowi] as $col=>$a_cell) {
$a_cell = str_ireplace('</TD>', '', $a_cell);
$cells[$rowi][$col] = $a_cell;
if ($rowi>1) {
$cells[$rowi][trim($cells[1][$col])] = $a_cell;
}
}
}
else {
return ('No cells');
}
}
}
else {
return ('No rows');
}
}
else {
'No html tables found';
}
return ($cells[2]);
}
?>