38 public static function compare($string1, $string2, $compareCharacters =
false)
42 if ($compareCharacters) {
43 $sequence1 = $string1;
44 $sequence2 = $string2;
45 $end1 = strlen($string1) - 1;
46 $end2 = strlen($string2) - 1;
48 $sequence1 = preg_split(
'/\R/', $string1);
49 $sequence2 = preg_split(
'/\R/', $string2);
50 $end1 = count($sequence1) - 1;
51 $end2 = count($sequence2) - 1;
55 while ($start <= $end1 && $start <= $end2
56 && $sequence1[$start] == $sequence2[$start]) {
61 while ($end1 >= $start && $end2 >= $start
62 && $sequence1[$end1] == $sequence2[$end2]) {
75 for ($index = 0; $index < $start; $index++) {
76 $diff[] = array($sequence1[$index], self::UNMODIFIED);
78 while (count($partialDiff) > 0) {
79 $diff[] = array_pop($partialDiff);
82 $end2 = ($compareCharacters ? strlen($sequence1) : count($sequence1));
83 for ($index = $end1 + 1; $index < $end2; $index++) {
84 $diff[] = array($sequence1[$index], self::UNMODIFIED);
102 $compareCharacters =
false
107 file_get_contents($file1),
108 file_get_contents($file2),
123 private static function computeTable($sequence1, $sequence2, $start, $end1, $end2)
126 $length1 = $end1 - $start + 1;
127 $length2 = $end2 - $start + 1;
130 $table = array(array_fill(0, $length2 + 1, 0));
133 for ($index1 = 1; $index1 <= $length1; $index1++) {
135 $table[$index1] = array(0);
138 for ($index2 = 1; $index2 <= $length2; $index2++) {
140 if ($sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]
142 $table[$index1][$index2] = $table[$index1 - 1][$index2 - 1] + 1;
144 $table[$index1][$index2] = max($table[$index1 - 1][$index2], $table[$index1][$index2 - 1]);
169 $index1 = count($table) - 1;
170 $index2 = count($table[0]) - 1;
173 while ($index1 > 0 || $index2 > 0) {
175 if ($index1 > 0 && $index2 > 0
176 && $sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]
179 $diff[] = array($sequence1[$index1 + $start - 1], self::UNMODIFIED);
182 } elseif ($index2 > 0
183 && $table[$index1][$index2] == $table[$index1][$index2 - 1]
186 $diff[] = array($sequence2[$index2 + $start - 1], self::INSERTED);
190 $diff[] = array($sequence1[$index1 + $start - 1], self::DELETED);
208 public static function toString($diff, $separator =
"\n")
214 foreach ($diff as $line) {
217 case self::UNMODIFIED:
218 $string .=
' '.$line[0];
221 $string .=
'- '.$line[0];
224 $string .=
'+ '.$line[0];
229 $string .= $separator;
245 public static function toHTML($diff, $separator =
'<br>')
251 $element =
'unknown';
252 foreach ($diff as $line) {
255 case self::UNMODIFIED:
283 public static function toTable($diff, $indentation =
'', $separator =
'<br>')
286 $html = $indentation.
"<table class=\"diff\">\n";
288 $rightCell = $leftCell =
'';
292 $nbdiff = count($diff);
293 while ($index < $nbdiff) {
295 switch ($diff[$index][1]) {
297 case self::UNMODIFIED:
305 $rightCell = $leftCell;
345 . ($leftCell == $rightCell
347 : ($leftCell ==
'' ?
'Blank' :
'Deleted'))
353 . ($leftCell == $rightCell
355 : ($rightCell ==
'' ?
'Blank' :
'Inserted'))
364 return $html.$indentation.
"</table>\n";
378 private static function getCellContent($diff, $indentation, $separator, &$index, $type)
384 while ($index < count($diff) && $diff[$index][1] == $type) {
387 . 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 specified 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...