Simple diff entre 2 fichiers

Description

Ceci est une simple fonction qui fait un diff sur 2 fichiers de type texte (texte = texte, php, html...enfin, tout sauf du binaire quoi).
Elle est sommaire, mais elle peut servir je pense. C'est juste un début sommaire pour un plus gros projet (un CVS en php, en fait). Je ne comptais pas la poster ici, mais finalement...je me dis qu'en l'état, elle doit pouvoir être utile à certains.

Source / Exemple :


<?php
/**

  • function fileDiff ()
  • checks differences between 2 files : additions, suppressions and modifications
  • @Param (string) sFileOriginal : the original filename
  • @Param (string) sFileModified : the modified filename
  • @Return (mixed) : false (boolean) if failed, (array) aDiff with differences if succeeded
  • /
function fileDiff ($sFileOriginal, $sFileModified) { /**
  • checks that the files are accessible. If not, exit.
  • /
if (!file_exists ($sFileOriginal) || !file_exists ($sFileModified)) { return false; } /**
  • gets the content of each file in an array, each entry being a line of the file
  • /
$aFirst = file ($sFileOriginal); $aLast = file ($sFileModified); /**
  • gets the biggest value between the size of both the arrays
  • /
$iMax = max (count ($aFirst), count ($aLast)); /**
  • creates a callback function to remove any \r\n\t or space at the end or the beginninh of the line
  • /
$myTrim = create_function ('$val', 'return trim ($val);'); /**
  • removes the empty entries in both the arrays, without touching the indexes
  • /
$aFirst = array_filter (array_map ($myTrim, $aFirst)); $aLast = array_filter (array_map ($myTrim, $aLast)); /**
  • declaration of the aDiff array
  • /
$aDiff = array (); /**
  • loops on both the arrays, to the end of the biggest one
  • /
for ($i = 0; $i < $iMax; $i++) { if (isset ($aFirst[$i])) { if (isset ($aLast[$i])) { if ($aFirst[$i] !== $aLast[$i]) { /**
  • both entries exists and are different, so, it's a modification
  • /
$aDiff[$i]['MODIFIED'] = htmlentities ($aLast[$i]); } } else { /**
  • there is an original entry, but nothing in the modified file, so, it's a suppression
  • /
$aDiff[$i]['SUPPR'] = htmlentities ($aFirst[$i]); } } else { if (isset ($aLast[$i])) { /**
  • there is a modified entry but no corresponding original entry, so, it's an addition
  • /
$aDiff[$i]['ADDED'] = htmlentities ($aLast[$i]); } } } return $aDiff; } $sFirst = 'test.php'; $sLast = 'test2.php'; $aDiff = fileDiff ($sFirst, $sLast); foreach ($aDiff as $clef => $val) { echo 'LIGNE ', $clef+1, ' : '; switch (key ($val)) { case 'MODIFIED' : echo '<span style="color: #0000ff;">',$val['MODIFIED'],'</span><br />'; break; case 'SUPPR' : echo '<span style="color: #ff0000;">',$val['SUPPR'],'</span><br />'; break; case 'ADDED' : echo '<span style="color: #00ff00;">',$val['ADDED'],'</span><br />'; break; } } ?>

Conclusion :


Ici les 2 fichiers utilisés en exemple (dispos dans le zip) :
test.php :
<?php
echo 'Hello World';

$a = 1+2;
$b = $a++;

echo $b;
?>

test2.php :
<?php
echo 'Hello World';

$a = 1+2;
$b = $a++;
$c = ++$b;
echo $c;

echo $d;
?>

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.