It's taken a bit, but I've finally managed to implement a change highlighter in my wiki script. The final approach is actually quite a bit simpler than my previous attempts and the result is much more accurate. I'm exceptionally excited about it simply because it was such a nuissance to implement
The only "problem" I have is when a file contains identical lines -- it doesn't detect adding/removing identical lines. Addressing this would, i think, be rather difficult and not particularly useful.
For the curious--
function compFiles($file1,$file2) {
$array1 = file($file1);
$array2 = file($file2);
$same = array_intersect($array1,$array2);
$same2 = array_intersect($array2,$array1);
$removed = $array1;
foreach($same as $num => $val ) {
unset($removed[$num]);
}
$added = $array2;
foreach($same2 as $num => $val) {
unset($added[$num]);
}
function compCol($value,$action) {
if ($action == 'add') $color = '#efe';
elseif ($action == 'rem') $color = '#fee';
else $color = '#fff';
$value = '<div style="background:'.$color.';">'.$value.'</div>';
return $value;
}
$same = array_intersect($array2,$same);
$added = array_intersect($array2,$added);
$counter = 0;
foreach ( $same as $num => $val ) {
array_splice($array2,$num,1,compCol($val,'sam'));
}
foreach ( $added as $num => $val ) {
array_splice($array2,$num,1,compCol($val,'add'));
}
foreach ( $removed as $num => $val ) {
array_splice($array2,$num+$counter,0,compCol($val,'rem'));
$counter++;
}
return(implode('',$array2));
}