37 public static function compare($string1, $string2, $compareCharacters =
false)
41 if ($compareCharacters) {
42 $sequence1 = $string1;
43 $sequence2 = $string2;
44 $end1 = strlen($string1) - 1;
45 $end2 = strlen($string2) - 1;
47 $sequence1 = preg_split(
'/\R/', $string1);
48 $sequence2 = preg_split(
'/\R/', $string2);
49 $end1 = count($sequence1) - 1;
50 $end2 = count($sequence2) - 1;
54 while ($start <= $end1 && $start <= $end2
55 && $sequence1[$start] == $sequence2[$start]) {
60 while ($end1 >= $start && $end2 >= $start
61 && $sequence1[$end1] == $sequence2[$end2]) {
74 for ($index = 0; $index < $start; $index++) {
75 $diff[] = array($sequence1[$index], self::UNMODIFIED);
77 while (count($partialDiff) > 0) {
78 $diff[] = array_pop($partialDiff);
81 $end2 = ($compareCharacters ? strlen($sequence1) : count($sequence1));
82 for ($index = $end1 + 1; $index < $end2; $index++) {
83 $diff[] = array($sequence1[$index], self::UNMODIFIED);
101 $compareCharacters =
false
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 $element =
'unknown';
251 foreach ($diff as $line) {
254 case self::UNMODIFIED:
282 public static function toTable($diff, $indentation =
'', $separator =
'<br>')
285 $html = $indentation.
"<table class=\"diff\">\n";
287 $rightCell = $leftCell =
'';
291 $nbdiff = count($diff);
292 while ($index < $nbdiff) {
294 switch ($diff[$index][1]) {
296 case self::UNMODIFIED:
304 $rightCell = $leftCell;
344 . ($leftCell == $rightCell
346 : ($leftCell ==
'' ?
'Blank' :
'Deleted'))
352 . ($leftCell == $rightCell
354 : ($rightCell ==
'' ?
'Blank' :
'Inserted'))
363 return $html.$indentation.
"</table>\n";
377 private static function getCellContent($diff, $indentation, $separator, &$index, $type)
383 while ($index < count($diff) && $diff[$index][1] == $type) {
386 . htmlspecialchars($diff[$index][0])
A class containing functions for computing diffs and formatting the output.
static compareFiles( $file1, $file2, $compareCharacters=false)
Returns the diff for two files.
static compare($string1, $string2, $compareCharacters=false)
Returns the diff for two strings.
static toString($diff, $separator="\n")
Returns a diff as a string, where unmodified lines are prefixed by ' ', deletions are prefixed by '- ...
static generatePartialDiff($table, $sequence1, $sequence2, $start)
Returns the partial diff for the specificed sequences, in reverse order.
static computeTable($sequence1, $sequence2, $start, $end1, $end2)
Returns the table of longest common subsequence lengths for the specified sequences.
static toHTML($diff, $separator='< br >')
Returns a diff as an HTML string, where unmodified lines are contained within 'span' elements,...
static getCellContent($diff, $indentation, $separator, &$index, $type)
Returns the content of the cell, for use in the toTable function.
static toTable($diff, $indentation='', $separator='< br >')
Returns a diff as an HTML table.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...