Skip to content

Commit 9ea1aac

Browse files
committed
Adds option for not creating a JSON file
1 parent c01f1ec commit 9ea1aac

3 files changed

Lines changed: 30 additions & 27 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ To use, include HTMLTable2JSON.php in your php file, create a new HTMLTable2JSON
5252
- Boolean indicating whether rows tagged with `style=\"display: none;` should appear in output.
5353
- Setting `TRUE` will suppress hidden rows.
5454
- Default: `FALSE`
55-
- `testing`
55+
- `printJSON`
56+
- Boolean indicating whether the program should create the JSON or simply return the output to the caller.
57+
- Setting `FALSE` leaves the output in the hands of the caller. `TRUE` creates a JSON file.
58+
- Default: `TRUE`
59+
- `testingTable`
5660
- String representing an HTML table. Allows user to manually input a table for conversion, instead of scraping from a webpage.
5761
- Ignores whatever value is in `url`.
58-
- Causes tableToJSON to return the JSON as a string (instead of creating a file with it).
5962
- Default: `NULL`
6063

6164
Note about php and optional arguments: If you wish to use an argument lower on the list, but not one higher, you must still fill in the higher values. To avoid changing the program, use `NULL` as the argument for any options you do not wish to change.

src/HTMLTable2JSON.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class HTMLTable2JSON {
1818

19-
public function tableToJSON($url, $firstColIsRowName = TRUE, $tableID = '', $ignoreCols = NULL, $headers = NULL, $firstRowIsData = FALSE, $onlyColumns = FALSE, $arrangeByRow = FALSE, $ignoreHidden = FALSE, $testing = NULL) {
19+
public function tableToJSON($url, $firstColIsRowName = TRUE, $tableID = '', $ignoreCols = NULL, $headers = NULL, $firstRowIsData = FALSE, $onlyColumns = FALSE, $arrangeByRow = FALSE, $ignoreHidden = FALSE, $printJSON = TRUE, $testingTable = NULL) {
2020
$ignoring = FALSE;
2121
$excluding = FALSE;
2222
if (NULL != $onlyColumns){
@@ -53,7 +53,7 @@ public function tableToJSON($url, $firstColIsRowName = TRUE, $tableID = '', $ign
5353
$headers = NULL;
5454
}
5555

56-
if (NULL == $testing) {
56+
if (NULL == $testingTable) {
5757
// Get html using curl
5858
$c = curl_init($url);
5959
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
@@ -69,7 +69,7 @@ public function tableToJSON($url, $firstColIsRowName = TRUE, $tableID = '', $ign
6969
die('Failed to get html from '.$url.'<br />');
7070
curl_close($c);
7171
}
72-
else $html = $testing;
72+
else $html = $testingTable;
7373

7474
// Pull table out of HTML
7575
if (strcmp('', $tableID))
@@ -301,7 +301,7 @@ public function tableToJSON($url, $firstColIsRowName = TRUE, $tableID = '', $ign
301301
$start_pos = stripos($table, '<tr');
302302
}
303303

304-
if (NULL == $testing) {
304+
if ($printJSON) {
305305
$outfile = 'output.json';
306306
if (false == ($out_handle = fopen($outfile, 'w')))
307307
die('Failed to create output file.');
@@ -331,7 +331,7 @@ public function tableToJSON($url, $firstColIsRowName = TRUE, $tableID = '', $ign
331331
$output = $output."\n}";
332332
}
333333

334-
if (NULL == $testing) {
334+
if ($printJSON) {
335335
fwrite($out_handle, $output);
336336
fclose($out_handle);
337337
echo 'JSON Created<br />';

test/run_tests.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
// basic usage with thead and tbody
5353
$table = "<table id=\"test-table\"><thead><tr><th>First Name</th><th>Last Name</th><th>Points</th></tr></thead><tbody><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></tbody></table>";
5454
$helper = new HTMLTable2JSON();
55-
$code_output = $helper->tableToJSON('', false, null, null, null, null, null, true, null, $table);
55+
$code_output = $helper->tableToJSON('', false, null, null, null, null, null, true, null, null, $table);
5656
$test_output = "[{\"First Name\":\"Jill\", \"Last Name\":\"Smith\", \"Points\":\"50\"},{\"First Name\":\"Eve\", \"Last Name\":\"Jackson\", \"Points\":\"94\"},{\"First Name\":\"John\", \"Last Name\":\"Doe\", \"Points\":\"80\"}]";
5757
if($code_output == $test_output)
5858
$passed++;
@@ -67,7 +67,7 @@
6767
// Tests: basic usage without thead or tbody
6868
$table = "<table id=\"test-table\"><tr><th>First Name</th><th>Last Name</th><th>Points</th></tr><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
6969
$helper = new HTMLTable2JSON();
70-
$code_output = $helper->tableToJSON('', false, null, null, null, null, null, true, null, $table);
70+
$code_output = $helper->tableToJSON('', false, null, null, null, null, null, true, null, null, $table);
7171
$test_output = "[{\"First Name\":\"Jill\", \"Last Name\":\"Smith\", \"Points\":\"50\"},{\"First Name\":\"Eve\", \"Last Name\":\"Jackson\", \"Points\":\"94\"},{\"First Name\":\"John\", \"Last Name\":\"Doe\", \"Points\":\"80\"}]";
7272
if($code_output == $test_output)
7373
$passed++;
@@ -87,7 +87,7 @@
8787
// Tests: ignore column 0
8888
$table = "<table id=\"test-table\"><thead><tr><th>First Name</th><th>Last Name</th><th>Points</th></tr></thead><tbody><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></tbody></table>";
8989
$helper = new HTMLTable2JSON();
90-
$code_output = $helper->tableToJSON('', false, null, array(0), null, null, null, true, null, $table);
90+
$code_output = $helper->tableToJSON('', false, null, array(0), null, null, null, true, null, null, $table);
9191
$test_output = "[{\"Last Name\":\"Smith\", \"Points\":\"50\"},{\"Last Name\":\"Jackson\", \"Points\":\"94\"},{\"Last Name\":\"Doe\", \"Points\":\"80\"}]";
9292
if($code_output == $test_output)
9393
$passed++;
@@ -101,7 +101,7 @@
101101
// Tests: Ignore hidden row
102102
$table = "<table id=\"test-table\"><thead><tr><th>First Name</th><th>Last Name</th><th>Points</th></tr></thead><tbody><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr style=\"display: none;\"><td>John</td><td>Doe</td><td>80</td></tr></tbody></table>";
103103
$helper = new HTMLTable2JSON();
104-
$code_output = $helper->tableToJSON('', false, null, array(0), null, null, null, true, true, $table);
104+
$code_output = $helper->tableToJSON('', false, null, array(0), null, null, null, true, true, null, $table);
105105
$test_output = "[{\"Last Name\":\"Smith\", \"Points\":\"50\"},{\"Last Name\":\"Jackson\", \"Points\":\"94\"}]";
106106

107107
if($code_output == $test_output)
@@ -115,8 +115,8 @@
115115
// Tests: Include hidden row
116116
$table = "<table id=\"test-table\"><thead><tr><th>First Name</th><th>Last Name</th><th>Points</th></tr></thead><tbody><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr style=\"display: none;\"><td>John</td><td>Doe</td><td>80</td></tr></tbody></table>";
117117
$helper = new HTMLTable2JSON();
118-
$code_output = $helper->tableToJSON(' ', false, null, array(0), null, null, null, true, false, $table);
119-
$another_output = $helper->tableToJSON(' ', false, null, array(0), null, null, null, true, null, $table);
118+
$code_output = $helper->tableToJSON(' ', false, null, array(0), null, null, null, true, false, null, $table);
119+
$another_output = $helper->tableToJSON(' ', false, null, array(0), null, null, null, true, null, null, $table);
120120
$test_output = "[{\"Last Name\":\"Smith\", \"Points\":\"50\"},{\"Last Name\":\"Jackson\", \"Points\":\"94\"},{\"Last Name\":\"Doe\", \"Points\":\"80\"}]";
121121
if($code_output == $test_output && $test_output == $another_output)
122122
$passed++;
@@ -129,7 +129,7 @@
129129
// Tests: Treat first row as column headers regardless of tags
130130
$table = "<table id=\"test-table\"><tr><td>First Name</td><td>Last Name</td><td>Points</td></tr><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
131131
$helper = new HTMLTable2JSON();
132-
$code_output = $helper->tableToJSON(' ', false, null, null, null, null, null, true, null, $table);
132+
$code_output = $helper->tableToJSON(' ', false, null, null, null, null, null, true, null, null, $table);
133133
$test_output = "[{\"First Name\":\"Jill\", \"Last Name\":\"Smith\", \"Points\":\"50\"},{\"First Name\":\"Eve\", \"Last Name\":\"Jackson\", \"Points\":\"94\"},{\"First Name\":\"John\", \"Last Name\":\"Doe\", \"Points\":\"80\"}]";
134134
if($code_output == $test_output)
135135
$passed++;
@@ -142,7 +142,7 @@
142142
// Tests: onlyColumns option - only columns 1 $ 2 (ignores column 0)
143143
$table = "<table id=\"test-table\"><thead><tr><th>First Name</th><th>Last Name</th><th>Points</th></tr></thead><tbody><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></tbody></table>";
144144
$helper = new HTMLTable2JSON();
145-
$code_output = $helper->tableToJSON('', false, null, null, null, null, array(1, 2), true, null, $table);
145+
$code_output = $helper->tableToJSON('', false, null, null, null, null, array(1, 2), true, null, null, $table);
146146
$test_output = "[{\"Last Name\":\"Smith\", \"Points\":\"50\"},{\"Last Name\":\"Jackson\", \"Points\":\"94\"},{\"Last Name\":\"Doe\", \"Points\":\"80\"}]";
147147
if($code_output == $test_output)
148148
$passed++;
@@ -156,7 +156,7 @@
156156
// Test uses only columns 1 & 2, ignore column 1 (should ignore column 0)
157157
$table = "<table id=\"test-table\"><thead><tr><th>First Name</th><th>Last Name</th><th>Points</th></tr></thead><tbody><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></tbody></table>";
158158
$helper = new HTMLTable2JSON();
159-
$code_output = $helper->tableToJSON('', false, null, array(1), null, null, array(1, 2), true, null, $table);
159+
$code_output = $helper->tableToJSON('', false, null, array(1), null, null, array(1, 2), true, null, null, $table);
160160
$test_output = "[{\"Last Name\":\"Smith\", \"Points\":\"50\"},{\"Last Name\":\"Jackson\", \"Points\":\"94\"},{\"Last Name\":\"Doe\", \"Points\":\"80\"}]";
161161
if($code_output == $test_output)
162162
$passed++;
@@ -171,7 +171,7 @@
171171
// Tests: include hidden row
172172
$table = "<table id=\"test-table\"><tr><th>First Name</th><th>Last Name</th><th>Points</th></tr><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
173173
$helper = new HTMLTable2JSON();
174-
$code_output = $helper->tableToJSON(' ', false, null, null, array(2 => 'Score'), null, null, true, null, $table);
174+
$code_output = $helper->tableToJSON(' ', false, null, null, array(2 => 'Score'), null, null, true, null, null, $table);
175175
$test_output = "[{\"First Name\":\"Jill\", \"Last Name\":\"Smith\", \"Score\":\"50\"},{\"First Name\":\"Eve\", \"Last Name\":\"Jackson\", \"Score\":\"94\"},{\"First Name\":\"John\", \"Last Name\":\"Doe\", \"Score\":\"80\"}]";
176176
if($code_output == $test_output)
177177
$passed++;
@@ -185,7 +185,7 @@
185185
// Note: You MUST set firstRowIsData to TRUE if there is not a header row in the table.
186186
$table = "<table id=\"test-table\"><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
187187
$helper = new HTMLTable2JSON();
188-
$code_output = $helper->tableToJSON(' ', false, null, null, array(0 => 'First Name', 1 => 'Last Name', 2 => 'Score'), TRUE, null, true, null, $table);
188+
$code_output = $helper->tableToJSON(' ', false, null, null, array(0 => 'First Name', 1 => 'Last Name', 2 => 'Score'), TRUE, null, true, null, null, $table);
189189
$test_output = "[{\"First Name\":\"Jill\", \"Last Name\":\"Smith\", \"Score\":\"50\"},{\"First Name\":\"Eve\", \"Last Name\":\"Jackson\", \"Score\":\"94\"},{\"First Name\":\"John\", \"Last Name\":\"Doe\", \"Score\":\"80\"}]";
190190
if($code_output == $test_output)
191191
$passed++;
@@ -197,11 +197,11 @@
197197

198198
// Tests: test when headers are not provided by the table, and are not provided as an argument
199199
$table = "<table id=\"test-table\"><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
200-
$code_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, null, true, null, $table);
200+
$code_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, null, true, null, null, $table);
201201
$table = "<table id=\"test-table\"><tr><th>Jill</th><th>Smith</th><th>50</th></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
202-
$another_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, null, true, null, $table);
202+
$another_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, null, true, null, null, $table);
203203
$table = "<table id=\"test-table\"><thead><tr><th>Jill</th><th>Smith</th><th>50</th></tr></thead><tbody><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></tbody></table>";
204-
$third_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, null, true, null, $table);
204+
$third_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, null, true, null, null, $table);
205205
$test_output = "[{\"Column 0\":\"Jill\", \"Column 1\":\"Smith\", \"Column 2\":\"50\"},{\"Column 0\":\"Eve\", \"Column 1\":\"Jackson\", \"Column 2\":\"94\"},{\"Column 0\":\"John\", \"Column 1\":\"Doe\", \"Column 2\":\"80\"}]";
206206
if($code_output == $test_output && $test_output == $another_output && $test_output == $third_output)
207207
$passed++;
@@ -213,11 +213,11 @@
213213

214214
// Tests: Interaction with ignoresColumns
215215
$table = "<table id=\"test-table\"><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
216-
$code_output = $helper->tableToJSON(' ', false, null, array(0), null, TRUE, null, true, null, $table);
216+
$code_output = $helper->tableToJSON(' ', false, null, array(0), null, TRUE, null, true, null, null, $table);
217217
$table = "<table id=\"test-table\"><tr><th>Jill</th><th>Smith</th><th>50</th></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
218-
$another_output = $helper->tableToJSON(' ', false, null, array(0), null, TRUE, null, true, null, $table);
218+
$another_output = $helper->tableToJSON(' ', false, null, array(0), null, TRUE, null, true, null, null, $table);
219219
$table = "<table id=\"test-table\"><thead><tr><th>Jill</th><th>Smith</th><th>50</th></tr></thead><tbody><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></tbody></table>";
220-
$third_output = $helper->tableToJSON(' ', false, null, array(0), null, TRUE, null, true, null, $table);
220+
$third_output = $helper->tableToJSON(' ', false, null, array(0), null, TRUE, null, true, null, null, $table);
221221
$test_output = "[{\"Column 1\":\"Smith\", \"Column 2\":\"50\"},{\"Column 1\":\"Jackson\", \"Column 2\":\"94\"},{\"Column 1\":\"Doe\", \"Column 2\":\"80\"}]";
222222
if($code_output == $test_output && $test_output == $another_output && $test_output == $third_output)
223223
$passed++;
@@ -229,11 +229,11 @@
229229

230230
// Tests: Interaction with onlyColumns
231231
$table = "<table id=\"test-table\"><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
232-
$code_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, array(1, 2), true, null, $table);
232+
$code_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, array(1, 2), true, null, null, $table);
233233
$table = "<table id=\"test-table\"><tr><th>Jill</th><th>Smith</th><th>50</th></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table>";
234-
$another_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, array(1, 2), true, null, $table);
234+
$another_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, array(1, 2), true, null, null, $table);
235235
$table = "<table id=\"test-table\"><thead><tr><th>Jill</th><th>Smith</th><th>50</th></tr></thead><tbody><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></tbody></table>";
236-
$third_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, array(1, 2), true, null, $table);
236+
$third_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, array(1, 2), true, null, null, $table);
237237
$test_output = "[{\"Column 1\":\"Smith\", \"Column 2\":\"50\"},{\"Column 1\":\"Jackson\", \"Column 2\":\"94\"},{\"Column 1\":\"Doe\", \"Column 2\":\"80\"}]";
238238
if($code_output == $test_output && $test_output == $another_output && $test_output == $third_output)
239239
$passed++;

0 commit comments

Comments
 (0)