3v4l.org

run code in 300+ PHP versions simultaneously
<?php function doCharDiff($from_text, $to_text) { $result = []; $jobs = [[0, strlen($from_text), 0, strlen($to_text)]]; while ($job = array_pop($jobs)) { // get the segments which must be diff'ed list($from_segment_start, $from_segment_end, $to_segment_start, $to_segment_end) = $job; $from_segment_len = $from_segment_end - $from_segment_start; $to_segment_len = $to_segment_end - $to_segment_start; // catch easy cases first if (!$from_segment_len || !$to_segment_len) { if ($from_segment_len) $result[$from_segment_start * 4 + 0] = new FineDiffDeleteOp($from_segment_len); else if ($to_segment_len) $result[$from_segment_start * 4 + 1] = new FineDiffInsertOp(substr($to_text, $to_segment_start, $to_segment_len)); continue; } if ($from_segment_len >= $to_segment_len) { $copy_len = $to_segment_len; while ($copy_len) { $to_copy_start = $to_segment_start; $to_copy_start_max = $to_segment_end - $copy_len; while ($to_copy_start <= $to_copy_start_max) { $from_copy_start = strpos(substr($from_text, $from_segment_start, $from_segment_len), substr($to_text, $to_copy_start, $copy_len)); if ($from_copy_start !== false) { $from_copy_start += $from_segment_start; break 2; } $to_copy_start++; } $copy_len--; } } else { $copy_len = $from_segment_len; while ($copy_len) { $from_copy_start = $from_segment_start; $from_copy_start_max = $from_segment_end - $copy_len; while ($from_copy_start <= $from_copy_start_max) { $to_copy_start = strpos(substr($to_text, $to_segment_start, $to_segment_len), substr($from_text, $from_copy_start, $copy_len)); if ($to_copy_start !== false) { $to_copy_start += $to_segment_start; break 2; } $from_copy_start++; } $copy_len--; } } // match found if ($copy_len) { $jobs[] = [$from_segment_start, $from_copy_start, $to_segment_start, $to_copy_start]; $result[$from_copy_start * 4 + 2] = new FineDiffCopyOp($copy_len); $jobs[] = [$from_copy_start + $copy_len, $from_segment_end, $to_copy_start + $copy_len, $to_segment_end]; } // no match, so delete all, insert all else { $result[$from_segment_start * 4] = new FineDiffReplaceOp($from_segment_len, substr($to_text, $to_segment_start, $to_segment_len)); } } ksort($result, SORT_NUMERIC); return array_values($result); }
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/NnOk1 on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/NnOk1 on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/NnOk1 on line 4
Process exited with code 255.

preferences:
234.57 ms | 401 KiB | 357 Q