3 use Caxy\HtmlDiff\HtmlDiff;
5 ini_set('display_errors', 1);
6 error_reporting(E_ALL);
8 require __DIR__.'/../vendor/autoload.php';
10 $debugOutput = array();
12 function addDebugOutput($value, $key = 'general')
16 if (!is_string($value)) {
17 $value = var_export($value, true);
20 if (!array_key_exists($key, $debugOutput)) {
21 $debugOutput[$key] = array();
24 $debugOutput[$key][] = $value;
27 $input = file_get_contents('php://input');
30 header('Content-Type: application/json');
32 $data = json_decode($input, true);
34 $oldText = $data['oldText'];
35 $newText = $data['newText'];
36 $useTableDiffing = isset($data['tableDiffing']) ? $data['tableDiffing'] : true;
38 $diff = new HtmlDiff($oldText, $newText, 'UTF-8', array());
39 if (array_key_exists('matchThreshold', $data)) {
40 $diff->setMatchThreshold($data['matchThreshold']);
42 $diff->setUseTableDiffing($useTableDiffing);
43 $diffOutput = $diff->build();
44 $diffOutput = iconv('UTF-8', 'UTF-8//IGNORE', $diffOutput);
46 $jsonOutput = json_encode(array('diff' => $diffOutput, 'debug' => $debugOutput));
48 if (false === $jsonOutput) {
49 throw new \Exception('Failed to encode JSON: '.json_last_error_msg());
54 header('Content-Type: text/html');
55 echo file_get_contents('demo.html');