Skip to content

Commit 82c52d2

Browse files
committed
strips URLs out of cell name
cell names now show up without the <a href="..." and </a> information. The URL contained in that area (if it exists) is added as another attribute of the cell (when cells are treated as discrete objects only).
1 parent 0c920b7 commit 82c52d2

4 files changed

Lines changed: 40 additions & 22 deletions

File tree

src/HTMLTable2JSON.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,19 @@ public function tableToJSON($url, $firstColIsRowName = TRUE, $tableID = '', $ign
220220
$cell_name = str_replace('"', '\"', $cell_name);
221221
$cell_name = str_replace('<br />', '', $cell_name);
222222
$cell_name = trim($cell_name);
223+
$link_start = stripos($cell_name, '<a href=\"');
224+
if (false === $link_start)
225+
$link = null;
226+
else {
227+
$link_start += + strlen('<a href=\"');
228+
$link_end = stripos($cell_name, '\">', $link_start);
229+
$link_len = $link_end - $link_start;
230+
$link = substr($cell_name, $link_start, $link_len);
231+
$link_start = stripos($cell_name, '</a>');
232+
$link_end += strlen('\">');
233+
$link_len = $link_start - $link_end;
234+
$cell_name = substr($cell_name, $link_end, $link_len);
235+
}
223236

224237
$other_pos = stripos($cell, ' rowspan-');
225238
if (false === $other_pos)
@@ -247,7 +260,7 @@ public function tableToJSON($url, $firstColIsRowName = TRUE, $tableID = '', $ign
247260
array_push($column_array, new TableColumn('Column'.count($column_array)));
248261
if (count($column_array) == $i)
249262
array_push($column_array, new TableColumn('Column '.$i));
250-
$column_array[$i]->addCell($cell_name, $row_header, $span_number);
263+
$column_array[$i]->addCell($cell_name, $row_header, $span_number, $link);
251264

252265
if($arrangeByRow){
253266
$column_header = $column_array[$i]->getName();

src/TableCell.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ class TableCell {
1515
private $column_info;
1616
private $row_info;
1717
private $row_span;
18+
private $url;
1819

19-
function __construct($text, $column, $row, $span){
20+
function __construct($text, $column, $row, $span, $url){
2021
$this->cell_text = $text;
2122
$this->column_info = $column;
2223
$this->row_info = $row;
2324
$this->row_span = $span;
25+
$this->url = $url;
2426
}
2527

2628
public function returnJSON(){
2729
$ret = "\n\t{\"cell_text\" : \"$this->cell_text\",\n";
30+
if (null != $this->url)
31+
$ret = $ret."\t\"url\" : \"$this->url\",\n";
2832
$ret = $ret."\t\"column\" : \"$this->column_info\",\n";
2933
$ret = $ret."\t\"row\" : \"$this->row_info\",\n";
3034
$ret = $ret."\t\"spans\" : \"$this->row_span\"}";

src/TableColumn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function hasCells(){
3333
return (count($this->cells) >= 1);
3434
}
3535

36-
public function addCell($cell_text, $row_header, $span){
37-
$new_cell = new TableCell($cell_text, $this->column_header, $row_header, $span);
36+
public function addCell($cell_text, $row_header, $span, $url){
37+
$new_cell = new TableCell($cell_text, $this->column_header, $row_header, $span, $url);
3838
array_unshift($this->cells, $new_cell);
3939
}
4040

test/run_tests.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121
if($code_output == $test_output)
2222
$passed++;
2323
else {
24-
echo 'test '.$tests.' failed.';
24+
echo '<br />test '.$tests.' failed.<br />';
2525
$failed++;
2626
}
2727
$tests++;
2828

2929

3030
// Tests: setting options
3131
// tables with some cells with rowspan > 1
32+
// parsing urls
3233
$helper->tableToJSON('http://kdic.grinnell.edu/programs/schedule/', true, 'wp-table-reloaded-id-6-no-1', array(0 => 0, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => 7, 7 => 8, 8 => 9));
3334
if (false == ($out_handle = fopen($outfile, 'w')))
3435
die('Failed to create output file.');
35-
$output = "{\"Monday\" : [\n\t{\"cell_text\" : \"<a href=\\\"http://kdic.grinnell.edu/programs/shows/the-jenn-n-juice-show/\\\">The Jenn N' Juice Show</a>\",\n\t\"column\" : \"Monday\",\n\t\"row\" : \"8:00PM\",\n\t\"spans\" : \"2\"},\n\t{\"cell_text\" : \"<a href=\\\"http://kdic.grinnell.edu/programs/shows/west-coast-dub/ \\\">West Coast Dub</a>\",\n\t\"column\" : \"Monday\",\n\t\"row\" : \"10:00PM\",\n\t\"spans\" : \"1\"},\n\t{\"cell_text\" : \"<a href=\\\"http://kdic.grinnell.edu/programs/shows/good-touches/\\\">Good Touches</a>\",\n\t\"column\" : \"Monday\",\n\t\"row\" : \"11:00PM\",\n\t\"spans\" : \"1\"},\n\t{\"cell_text\" : \"<a href=\\\"http://kdic.grinnell.edu/programs/shows/the-ray-kdic-variety-hour/\\\">The Ray KDIC Variety Hour</a>\",\n\t\"column\" : \"Monday\",\n\t\"row\" : \"12:00AM\",\n\t\"spans\" : \"1\"}]\n}";
36+
$output = "{\"Monday\" : [\n\t{\"cell_text\" : \"The Jenn N' Juice Show\",\n\t\"url\" : \"http://kdic.grinnell.edu/programs/shows/the-jenn-n-juice-show/\",\n\t\"column\" : \"Monday\",\n\t\"row\" : \"8:00PM\",\n\t\"spans\" : \"2\"},\n\t{\"cell_text\" : \"West Coast Dub\",\n\t\"url\" : \"http://kdic.grinnell.edu/programs/shows/west-coast-dub/ \",\n\t\"column\" : \"Monday\",\n\t\"row\" : \"10:00PM\",\n\t\"spans\" : \"1\"},\n\t{\"cell_text\" : \"Good Touches\",\n\t\"url\" : \"http://kdic.grinnell.edu/programs/shows/good-touches/\",\n\t\"column\" : \"Monday\",\n\t\"row\" : \"11:00PM\",\n\t\"spans\" : \"1\"},\n\t{\"cell_text\" : \"The Ray KDIC Variety Hour\",\n\t\"url\" : \"http://kdic.grinnell.edu/programs/shows/the-ray-kdic-variety-hour/\",\n\t\"column\" : \"Monday\",\n\t\"row\" : \"12:00AM\",\n\t\"spans\" : \"1\"}]\n}";
3637
fwrite($out_handle, $output);
3738
fclose($out_handle);
3839

@@ -41,7 +42,7 @@
4142
if($code_output == $test_output)
4243
$passed++;
4344
else {
44-
echo '<br />test '.$tests.' failed.';
45+
echo '<br />test '.$tests.' failed.<br />';
4546
$failed++;
4647
}
4748
$tests++;
@@ -56,7 +57,7 @@
5657
if($code_output == $test_output)
5758
$passed++;
5859
else {
59-
echo '<br />test '.$tests.' failed.';
60+
echo '<br />test '.$tests.' failed.<br />';
6061
$failed++;
6162
}
6263
$tests++;
@@ -71,7 +72,7 @@
7172
if($code_output == $test_output)
7273
$passed++;
7374
else {
74-
echo '<br />test '.$tests.' failed.';
75+
echo '<br />test '.$tests.' failed.<br />';
7576
$failed++;
7677
}
7778
$tests++;
@@ -91,7 +92,7 @@
9192
if($code_output == $test_output)
9293
$passed++;
9394
else {
94-
echo '<br />test '.$tests.' failed.';
95+
echo '<br />test '.$tests.' failed.<br />';
9596
$failed++;
9697
}
9798
$tests++;
@@ -106,7 +107,7 @@
106107
if($code_output == $test_output)
107108
$passed++;
108109
else {
109-
echo '<br />test '.$tests.' failed.';
110+
echo '<br />test '.$tests.' failed.<br />';
110111
$failed++;
111112
}
112113
$tests++;
@@ -120,7 +121,7 @@
120121
if($code_output == $test_output && $test_output == $another_output)
121122
$passed++;
122123
else {
123-
echo '<br />test '.$tests.' failed.';
124+
echo '<br />test '.$tests.' failed.<br />';
124125
$failed++;
125126
}
126127
$tests++;
@@ -133,7 +134,7 @@
133134
if($code_output == $test_output)
134135
$passed++;
135136
else {
136-
echo '<br />test '.$tests.' failed.';
137+
echo '<br />test '.$tests.' failed.<br />';
137138
$failed++;
138139
}
139140
$tests++;
@@ -146,7 +147,7 @@
146147
if($code_output == $test_output)
147148
$passed++;
148149
else {
149-
echo '<br />test '.$tests.' failed.';
150+
echo '<br />test '.$tests.' failed.<br />';
150151
$failed++;
151152
}
152153
$tests++;
@@ -160,7 +161,7 @@
160161
if($code_output == $test_output)
161162
$passed++;
162163
else {
163-
echo '<br />test '.$tests.' failed.';
164+
echo '<br />test '.$tests.' failed.<br />';
164165
$failed++;
165166
}
166167
$tests++;
@@ -175,7 +176,7 @@
175176
if($code_output == $test_output)
176177
$passed++;
177178
else {
178-
echo '<br />test '.$tests.' failed.';
179+
echo '<br />test '.$tests.' failed.<br />';
179180
$failed++;
180181
}
181182
$tests++;
@@ -189,7 +190,7 @@
189190
if($code_output == $test_output)
190191
$passed++;
191192
else {
192-
echo '<br />test '.$tests.' failed.';
193+
echo '<br />test '.$tests.' failed.<br />';
193194
$failed++;
194195
}
195196
$tests++;
@@ -205,7 +206,7 @@
205206
if($code_output == $test_output && $test_output == $another_output && $test_output == $third_output)
206207
$passed++;
207208
else {
208-
echo '<br />test '.$tests.' failed.';
209+
echo '<br />test '.$tests.' failed.<br />';
209210
$failed++;
210211
}
211212
$tests++;
@@ -221,7 +222,7 @@
221222
if($code_output == $test_output && $test_output == $another_output && $test_output == $third_output)
222223
$passed++;
223224
else {
224-
echo '<br />test '.$tests.' failed.';
225+
echo '<br />test '.$tests.' failed.<br />';
225226
$failed++;
226227
}
227228
$tests++;
@@ -237,7 +238,7 @@
237238
if($code_output == $test_output && $test_output == $another_output && $test_output == $third_output)
238239
$passed++;
239240
else {
240-
echo '<br />test '.$tests.' failed.';
241+
echo '<br />test '.$tests.' failed.<br />';
241242
$failed++;
242243
}
243244
$tests++;
@@ -250,7 +251,7 @@
250251
$helper->tableToJSON('http://kdic.grinnell.edu/programs/schedule/', TRUE, null, null, null, false, array(1), true);
251252
if (false == ($out_handle = fopen($outfile, 'w')))
252253
die('Failed to create output file.');
253-
$output = "{\t\"8:00PM\":{\n\t\t\"Monday\":\"<a href=\\\"http://kdic.grinnell.edu/programs/shows/the-jenn-n-juice-show/\\\">The Jenn N' Juice Show</a>\"\n\t},\n\t\"10:00PM\":{\n\t\t\"Monday\":\"<a href=\\\"http://kdic.grinnell.edu/programs/shows/west-coast-dub/ \\\">West Coast Dub</a>\"\n\t},\n\t\"11:00PM\":{\n\t\t\"Monday\":\"<a href=\\\"http://kdic.grinnell.edu/programs/shows/good-touches/\\\">Good Touches</a>\"\n\t},\n\t\"12:00AM\":{\n\t\t\"Monday\":\"<a href=\\\"http://kdic.grinnell.edu/programs/shows/the-ray-kdic-variety-hour/\\\">The Ray KDIC Variety Hour</a>\"\n\t}\n}";
254+
$output = "{\t\"8:00PM\":{\n\t\t\"Monday\":\"The Jenn N' Juice Show\"\n\t},\n\t\"10:00PM\":{\n\t\t\"Monday\":\"West Coast Dub\"\n\t},\n\t\"11:00PM\":{\n\t\t\"Monday\":\"Good Touches\"\n\t},\n\t\"12:00AM\":{\n\t\t\"Monday\":\"The Ray KDIC Variety Hour\"\n\t}\n}";
254255
fwrite($out_handle, $output);
255256
fclose($out_handle);
256257

@@ -259,7 +260,7 @@
259260
if($code_output == $test_output)
260261
$passed++;
261262
else {
262-
echo '<br />test '.$tests.' failed.';
263+
echo '<br />test '.$tests.' failed.<br />';
263264
$failed++;
264265
}
265266
$tests++;

0 commit comments

Comments
 (0)