topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Wednesday April 24, 2024, 8:56 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Highlighting Changes in Files  (Read 3405 times)

allen

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,206
    • View Profile
    • Donate to Member
Highlighting Changes in Files
« on: March 01, 2006, 10:10 PM »
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));
}