Simple Diff
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 99 100101 102 103 104 105106 107 108 109 110111 112 113 114 115116 117 118 119 120121 122 123 124 125126 127 128 129 130131 132 133 | <?php /** * This class provides a simple diff function. * * @package * @category * @author Dave Marshall * @author $Author: $ * @version $Rev: $ * @since $Date: $ * @link $URL: $ */ class My_Diff { /** * Method to find longest common subsequences, based on * http://en.wikipedia.org/wiki/Longest_common_subsequence_problem * * @param string $s1 * @param string $s2 * @return array * @see http://en.wikipedia.org/wiki/Longest_common_subsequence_problem */ protected function lsm($s1, $s2) { $mStart = 0; $mEnd = count($s1) - 1; $nStart = 0; $nEnd = count($s2) - 1; $c = array(); for($i = -1; $i <= $mEnd; $i++) { $c[$i] = array(); for($j = -1; $j <= $nEnd; $j++) { $c[$i][$j] = 0; } } for($i = $mStart; $i <= $mEnd; $i++) { for($j = $nStart; $j <= $nEnd; $j++) { if ($s1[$i] == $s2[$j]) { $c[$i][$j] = $c[$i -1][$j - 1] + 1; } else { $c[$i][$j] = max($c[$i][$j - 1], $c[$i - 1][$j]); } } } return $c; } /** * Simple formatting of the array created by the <tt>lsm</tt> method. * Lines are printed as normal, lines that are only in the second string are * prefixed with '+', lines that are only in the first string are prefixed * with '-' * * @param array $c Output of <tt>lsm</tt> method * @param string First string * @param string Second String * @param int $i * @param int $j * @return string * @see lsm */ protected function printDiff($c, $s1, $s2, $i, $j) { $diff = ""; if ($i >= 0 && $j >= 0 && $s1[$i] == $s2[$j]) { $diff .= $this->printDiff($c, $s1, $s2, $i - 1, $j - 1); $diff .= " " . $s1[$i] . PHP_EOL; } else { if ($j >= 0 && ($i == -1 || $c[$i][$j - 1] >= $c[$i - 1][$j])) { $diff .= $this->printDiff($c, $s1, $s2, $i, $j - 1); $diff .= "+ " . $s2[$j] . PHP_EOL; } else if ($i >= 0 && ($j == -1 || $c[$i][$j - 1] < $c[$i - 1][$j])) { $diff .= $this->printDiff($c, $s1, $s2, $i - 1, $j); $diff .= "- " . $s1[$i] . PHP_EOL; } } return $diff; } /** * Given two strings, returns a string in the format describe by * My_Diff::printDiff * * @param string $s1 First String * @param string $s2 Second String * @return string */ public function diff($s1, $s2) { $s1 = split("n", $s1); $s2 = split("n", $s2); return $this->printDiff($this->lsm($s1, $s2), $s1, $s2, count($s1) - 1, count($s2) - 1); } } ## Example usage $s1 = ' if ($j >= 0 && ($i == -1 || $c[$i][$j - 1] >= $c[$i - 1][$j])) { $diff .= $this->printDiff($c, $s1, $s2, $i, $j - 1); $diff .= "+ " . $s2[$j] . PHP_EOL; } else if ($i >= 0 && ($j == -1 || $c[$i][$j - 1] < $c[$i - 1][$j])) { $diff .= $this->printDiff($c, $s1, $s2, $i - 1, $j); $diff .= "- " . $s1[$i] . PHP_EOL; } '; $s2 = ' if ($j >= 0 && ($i == -1 || $c[$i][$j - 1] >= $c[$i - 1][$j])) { $diff .= $this->printDiff($c, $s1, $s2, $i, $j - 1); $diff .= "> " . $s2[$j] . PHP_EOL; } else if ($i >= 0 && ($j == -1 || $c[$i][$j - 1] < $c[$i - 1][$j])) { $diff .= $this->printDiff($c, $s1, $s2, $i - 1, $j); $diff .= "< " . $s1[$i] . PHP_EOL; } '; $diff = new My_Diff; echo $diff->diff($s1, $s2); ## Output if ($j >= 0 && ($i == -1 || $c[$i][$j - 1] >= $c[$i - 1][$j])) { $diff .= $this->printDiff($c, $s1, $s2, $i, $j - 1); - $diff .= "+ " . $s2[$j] . PHP_EOL; + $diff .= "> " . $s2[$j] . PHP_EOL; } else if ($i >= 0 && ($j == -1 || $c[$i][$j - 1] < $c[$i - 1][$j])) { $diff .= $this->printDiff($c, $s1, $s2, $i - 1, $j);- $diff .= "- " . $s1[$i] . PHP_EOL; + $diff .= "< " . $s1[$i] . PHP_EOL; } |
Comments
@davedevelopment This class can be static, I mean all methods can be static, since you dont hold any data into, and you don't need to instantiate it.
So, instead
$diff = new My_Diff;
echo $diff->diff($s1, $s2);
you will have
echo My_Diff::diff($s1, $s2);
@umpirsky Initially it was static, but I started thinking about making it a little configurable.
$diff = new My_Diff();
$diff->setAddPrefix = '<span class="add">';
$diff->setDelPrefix = '<span class="del">';
$diff->setSuffix = '</span>';
echo $diff->($s1, $s2);
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
This is a very simple diff class, used to show the diffs on this site. I appreciate it's not exactly Zend Framework specific, but what the hell, this is my website ;)
It's based on the algorithms found at http://en.wikipedia.org/wiki/Longest_common_subsequence_problem, but doesn't go as far to include some of the optimisations etc.
It would be nice to extend it to create other formats, as well as improve the performance but this will do for now.
Snippet details
- Created:
-
davedevelopment
- Edited:
-
davedevelopment
- Revision Id:
- 55
- Edit Message:
- Added example usage and expected output
- Tags:
- diff
- Comments:
- 6
- Views:
- 164
- Points:
- 3 (3 votes)
1 year ago
Any example?