1
|
|
|
<?php |
2
|
|
|
// from https://github.com/wdalmut/php-diff |
3
|
|
|
// changes - changed by Richard Griffith to use in gwiki |
4
|
|
|
// changes - add github comment, changes comments and comment out namespace |
5
|
|
|
//namespace Wally; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class provides a simple diff function. |
9
|
|
|
* |
10
|
|
|
* Refactored for personal scope on base written by Dave Marshall |
11
|
|
|
* |
12
|
|
|
* @package Wally |
13
|
|
|
* @author Walter Dal Mut |
14
|
|
|
*/ |
15
|
|
|
class Diff |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Method to find longest common subsequences, based on |
19
|
|
|
* http://en.wikipedia.org/wiki/Longest_common_subsequence_problem |
20
|
|
|
* |
21
|
|
|
* @param string $s1 |
22
|
|
|
* @param string $s2 |
23
|
|
|
* @return array |
24
|
|
|
* @see http://en.wikipedia.org/wiki/Longest_common_subsequence_problem |
25
|
|
|
*/ |
26
|
|
|
protected function _lsm($s1, $s2) |
27
|
|
|
{ |
28
|
|
|
$mStart = 0; |
29
|
|
|
$mEnd = count($s1) - 1; |
30
|
|
|
$nStart = 0; |
31
|
|
|
$nEnd = count($s2) - 1; |
32
|
|
|
$c = array(); |
33
|
|
|
for ($i = -1; $i <= $mEnd; ++$i) { |
34
|
|
|
$c[$i] = array(); |
35
|
|
|
for ($j = -1; $j <= $nEnd; ++$j) { |
36
|
|
|
$c[$i][$j] = 0; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
for ($i = $mStart; $i <= $mEnd; ++$i) { |
40
|
|
|
for ($j = $nStart; $j <= $nEnd; ++$j) { |
41
|
|
|
if ($s1[$i] === $s2[$j]) { |
42
|
|
|
$c[$i][$j] = $c[$i - 1][$j - 1] + 1; |
43
|
|
|
} else { |
44
|
|
|
$c[$i][$j] = max($c[$i][$j - 1], $c[$i - 1][$j]); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $c; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Simple formatting of the array created by the <tt>lsm</tt> method. |
54
|
|
|
* Lines are printed as normal, lines that are only in the second string are |
55
|
|
|
* prefixed with '+', lines that are only in the first string are prefixed |
56
|
|
|
* with '-' |
57
|
|
|
* |
58
|
|
|
* @param array $c Output of <tt>lsm</tt> method |
59
|
|
|
* @param string First string |
60
|
|
|
* @param string Second String |
61
|
|
|
* @param int $i |
62
|
|
|
* @param int $j |
63
|
|
|
* @return string |
64
|
|
|
* @see lsm |
65
|
|
|
*/ |
66
|
|
|
protected function _printDiff($c, $s1, $s2, $i, $j) |
67
|
|
|
{ |
68
|
|
|
$diff = ''; |
69
|
|
|
if ($i >= 0 && $j >= 0 && $s1[$i] === $s2[$j]) { |
70
|
|
|
$diff .= $this->_printDiff($c, $s1, $s2, $i - 1, $j - 1); |
71
|
|
|
$diff .= ' ' . $s1[$i] . PHP_EOL; |
72
|
|
|
} else { |
73
|
|
|
if ($j >= 0 && ($i === -1 || $c[$i][$j - 1] >= $c[$i - 1][$j])) { |
74
|
|
|
$diff .= $this->_printDiff($c, $s1, $s2, $i, $j - 1); |
75
|
|
|
$diff .= '+ ' . $s2[$j] . PHP_EOL; |
76
|
|
|
} elseif ($i >= 0 && ($j === -1 || $c[$i][$j - 1] < $c[$i - 1][$j])) { |
77
|
|
|
$diff .= $this->_printDiff($c, $s1, $s2, $i - 1, $j); |
78
|
|
|
$diff .= '- ' . $s1[$i] . PHP_EOL; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $diff; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Given two strings, returns a string in the format describe by |
87
|
|
|
* Wally\Diff::printDiff |
88
|
|
|
* |
89
|
|
|
* @param string $s1 First String |
90
|
|
|
* @param string $s2 Second String |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getDiff($s1, $s2) |
94
|
|
|
{ |
95
|
|
|
$s1 = explode("\n", $s1); |
96
|
|
|
$s2 = explode("\n", $s2); |
97
|
|
|
|
98
|
|
|
return $this->_printDiff($this->_lsm($s1, $s2), $s1, $s2, count($s1) - 1, count($s2) - 1); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: