36 public static function compare($string1, $string2, $compareCharacters =
false)
40 if ($compareCharacters) {
41 $sequence1 = $string1;
42 $sequence2 = $string2;
43 $end1 = strlen($string1) - 1;
44 $end2 = strlen($string2) - 1;
46 $sequence1 = preg_split(
'/\R/', $string1);
47 $sequence2 = preg_split(
'/\R/', $string2);
48 $end1 = count($sequence1) - 1;
49 $end2 = count($sequence2) - 1;
53 while ($start <= $end1 && $start <= $end2
54 && $sequence1[$start] == $sequence2[$start]) {
59 while ($end1 >= $start && $end2 >= $start
60 && $sequence1[$end1] == $sequence2[$end2]) {
66 $table = self::computeTable($sequence1, $sequence2, $start, $end1, $end2);
69 $partialDiff = self::generatePartialDiff($table, $sequence1, $sequence2, $start);
73 for ($index = 0; $index < $start; $index++) {
74 $diff[] = array($sequence1[$index], self::UNMODIFIED);
76 while (count($partialDiff) > 0) {
77 $diff[] = array_pop($partialDiff);
80 $end2 = ($compareCharacters ? strlen($sequence1) : count($sequence1));
81 for ($index = $end1 + 1; $index < $end2; $index++)
83 $diff[] = array($sequence1[$index], self::UNMODIFIED);
101 $compareCharacters =
false
105 return self::compare(
106 file_get_contents($file1),
107 file_get_contents($file2),
122 private static function computeTable($sequence1, $sequence2, $start, $end1, $end2)
125 $length1 = $end1 - $start + 1;
126 $length2 = $end2 - $start + 1;
129 $table = array(array_fill(0, $length2 + 1, 0));
132 for ($index1 = 1; $index1 <= $length1; $index1++) {
134 $table[$index1] = array(0);
137 for ($index2 = 1; $index2 <= $length2; $index2++) {
139 if ($sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]
141 $table[$index1][$index2] = $table[$index1 - 1][$index2 - 1] + 1;
143 $table[$index1][$index2] = max($table[$index1 - 1][$index2], $table[$index1][$index2 - 1]);
168 $index1 = count($table) - 1;
169 $index2 = count($table[0]) - 1;
172 while ($index1 > 0 || $index2 > 0) {
174 if ($index1 > 0 && $index2 > 0
175 && $sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]
178 $diff[] = array($sequence1[$index1 + $start - 1], self::UNMODIFIED);
181 } elseif ($index2 > 0
182 && $table[$index1][$index2] == $table[$index1][$index2 - 1]
185 $diff[] = array($sequence2[$index2 + $start - 1], self::INSERTED);
189 $diff[] = array($sequence1[$index1 + $start - 1], self::DELETED);
207 public static function toString($diff, $separator =
"\n")
213 foreach ($diff as $line) {
216 case self::UNMODIFIED:
217 $string .=
' '.$line[0];
220 $string .=
'- '.$line[0];
223 $string .=
'+ '.$line[0];
228 $string .= $separator;
244 public static function toHTML($diff, $separator =
'<br>')
250 foreach ($diff as $line) {
253 case self::UNMODIFIED:
265 . htmlspecialchars($line[0])
284 public static function toTable($diff, $indentation =
'', $separator =
'<br>')
287 $html = $indentation.
"<table class=\"diff\">\n";
291 while ($index < count($diff)) {
293 switch ($diff[$index][1]) {
295 case self::UNMODIFIED:
296 $leftCell = self::getCellContent(
303 $rightCell = $leftCell;
308 $leftCell = self::getCellContent(
315 $rightCell = self::getCellContent(
327 $rightCell = self::getCellContent(
343 . ($leftCell == $rightCell
345 : ($leftCell ==
'' ?
'Blank' :
'Deleted'))
351 . ($leftCell == $rightCell
353 : ($rightCell ==
'' ?
'Blank' :
'Inserted'))
362 return $html.$indentation.
"</table>\n";
376 private static function getCellContent($diff, $indentation, $separator, &$index, $type)
382 while ($index < count($diff) && $diff[$index][1] == $type) {
385 . htmlspecialchars($diff[$index][0])
static toHTML($diff, $separator= '< br >')
Returns a diff as an HTML string, where unmodified lines are contained within 'span' elements...
static generatePartialDiff($table, $sequence1, $sequence2, $start)
Returns the partial diff for the specificed sequences, in reverse order.
static compare($string1, $string2, $compareCharacters=false)
Returns the diff for two strings.
static computeTable($sequence1, $sequence2, $start, $end1, $end2)
Returns the table of longest common subsequence lengths for the specified sequences.
static toTable($diff, $indentation= '', $separator= '< br >')
Returns a diff as an HTML table.
A class containing functions for computing diffs and formatting the output.
static getCellContent($diff, $indentation, $separator, &$index, $type)
Returns the content of the cell, for use in the toTable function.
static compareFiles($file1, $file2, $compareCharacters=false)
Returns the diff for two files.
static toString($diff, $separator="\n")
Returns a diff as a string, where unmodified lines are prefixed by ' ', deletions are prefixed by '- ...