$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>"; $helper = new HTMLTable2JSON(); $code_output = $helper->tableToJSON(' ', false, null, null, array(2 => 'Score'), null, null, true, null, null, $table); $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\"}]"; if ($code_output == $test_output) { $passed++; } else { echo '<br />test ' . $tests . ' failed.<br />'; $failed++; } $tests++; // Tests: test when headers are not provided by the table, but ARE provided as an argument // Note: You MUST set firstRowIsData to TRUE if there is not a header row in the table. $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>"; $helper = new HTMLTable2JSON(); $code_output = $helper->tableToJSON(' ', false, null, null, array(0 => 'First Name', 1 => 'Last Name', 2 => 'Score'), TRUE, null, true, null, null, $table); $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\"}]"; if ($code_output == $test_output) { $passed++; } else { echo '<br />test ' . $tests . ' failed.<br />'; $failed++; } $tests++; // Tests: test when headers are not provided by the table, and are not provided as an argument $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>"; $code_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, null, true, null, null, $table); $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>"; $another_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, null, true, null, null, $table); $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>"; $third_output = $helper->tableToJSON(' ', false, null, null, null, TRUE, null, true, null, null, $table);
<?php include_once 'HTMLTable2JSON.php'; $helper = new HTMLTable2JSON(); // Standard Usage $helper->tableToJSON('http://kdic.grinnell.edu/programs/schedule/'); // Explicitly filling some fields: These two samples are identical //$helper->tableToJSON('http://kdic.grinnell.edu/programs/schedule/', TRUE, 'wp-table-reloaded-id-6-no-1', array(0 => 5)); //$helper->tableToJSON('http://kdic.grinnell.edu/programs/schedule/', NULL, NULL, array(0 => 5)); // Treating first row as data //$helper->tableToJSON('http://lightswitch05.github.io/table-to-json/', FALSE); // Using $testing //$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>"; //$output = $helper->tableToJSON('', false, null, null, $table);