27 preg_match_all(
"/(<.*?>\\s*|\\s+)([^\\s<]*)/",
" ".$a, $matchA);
28 preg_match_all(
"/(<.*?>\\s*|\\s+)([^\\s<]*)/",
" ".$b, $matchB);
31 if(empty($diffScript))
40 foreach($diffScript as $diffItem)
42 while($positionA < $diffItem[
'startA'])
44 $result .= $matchA[0][$positionA];
49 if($diffItem[
'deletedA'] > 0)
51 $result .= $matchA[1][$positionA] .
'<s style="color:red">' . $matchA[2][$positionA];
52 for($i = 1; $i < $diffItem[
'deletedA']; $i++)
53 $result .= $matchA[0][$positionA + $i];
56 $positionA = $positionA + $diffItem[
'deletedA'];
59 if($diffItem[
'insertedB'] > 0)
61 $result .= $matchB[1][$diffItem[
'startB']] .
'<b style="color:green">' . $matchB[2][$diffItem[
'startB']];
62 for($i = 1; $i < $diffItem[
'insertedB']; $i++)
63 $result .= $matchB[0][$diffItem[
'startB'] + $i];
69 while($positionA < count($matchA[0]))
71 $result .= $matchA[0][$positionA];
123 while ($lowerA < $upperA && $lowerB < $upperB && $a[$lowerA] == $b[$lowerB])
130 while ($lowerA < $upperA && $lowerB < $upperB && $a[$upperA - 1] == $b[$upperB - 1])
136 if ($lowerA === $upperA)
139 while ($lowerB < $upperB)
141 $this->modifiedB[$lowerB++] =
true;
146 if ($lowerB === $upperB)
149 while ($lowerA < $upperA)
151 $this->modifiedA[$lowerA++] =
true;
183 $downK = $lowerA - $lowerB;
184 $upK = $upperA - $upperB;
186 $delta = ($upperA - $lowerA) - ($upperB - $lowerB);
187 $oddDelta = ($delta & 1) != 0;
189 $maxD = (($upperA - $lowerA + $upperB - $lowerB) / 2) + 1;
192 $this->downVector[$downK + 1] = $lowerA;
193 $this->upVector[$upK - 1] = $upperA;
195 for ($d = 0; $d <= $maxD; $d++)
199 for ($k = $downK - $d; $k <= $downK + $d; $k += 2)
204 if ($k == $downK - $d)
206 $x = $this->downVector[$k + 1];
210 $x = $this->downVector[$k - 1] + 1;
211 if (($k < $downK + $d) && ($this->downVector[$k + 1] >= $x))
213 $x = $this->downVector[$k + 1];
219 while (($x < $upperA) && ($y < $upperB) && ($a[$x] == $b[$y]))
224 $this->downVector[$k] = $x;
227 if ($oddDelta && ($upK - $d < $k) && ($k < $upK + $d))
229 if ($this->upVector[$k] <= $this->downVector[$k])
231 $result[
'x'] = $this->downVector[$k];
232 $result[
'y'] = $this->downVector[$k] - $k;
239 for ($k = $upK - $d; $k <= $upK + $d; $k += 2)
247 $x = $this->upVector[$k - 1];
251 $x = $this->upVector[$k + 1] - 1;
252 if (($k > $upK - $d) && ($this->upVector[$k - 1] < $x))
254 $x = $this->upVector[$k - 1];
259 while (($x > $lowerA) && ($y > $lowerB) && ($a[$x - 1] == $b[$y - 1]))
265 $this->upVector[$k] = $x;
268 if (!$oddDelta && ($downK - $d <= $k) && ($k <= $downK + $d))
270 if ($this->upVector[$k] <= $this->downVector[$k])
272 $result[
'x'] = $this->downVector[$k];
273 $result[
'y'] = $this->downVector[$k] - $k;
292 while ($indexA < count($a) || $indexB < count($b))
294 if (($indexA < count($a)) && (empty($this->modifiedA[$indexA])) && ($indexB < count($b)) && empty($this->modifiedB[$indexB]))
306 while ($indexA < count($a) && ($indexB >= count($b) || !empty($this->modifiedA[$indexA])))
311 while ($indexB < count($b) && ($indexA >= count($a) || !empty($this->modifiedB[$indexB])))
316 if (($startA < $indexA) || ($startB < $indexB))
319 $result[] = array(
"startA" => $startA,
"startB" => $startB,
"deletedA" => $indexA - $startA,
"insertedB" => $indexB - $startB);