-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhtml2cell.php
79 lines (77 loc) · 2.45 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
<?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 {
return ('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 {
return ('No html tables found');
}
return ($cells[2]);
}
?>