Location: PHPKode > scripts > Very Simple Diff > verySimpleDiff.class.php
<?php

// To use this function, use the following code...
// include("verySimpleDiff.class.php");
// $verySimpleDiff = new verySimpleDiff();
// $results = $verySimpleDiff->diff($oldData, $newData);

// To customize the $strAdded and $strRemoved strings, use
// the following code...
// $verySimpleDiff->strAdded = "Added ";
// $verySimpleDiff->strRemoved = "Removed ";

// If you want the function to detect simple line swaps
// in the data comparison and difference checking routine
// add this as part of the execution code...
// $verySimpleDiff->detectLineSwaps = true;

class verySimpleDiff {
	var $strAdded = "Added ";
	var $strRemoved = "Removed ";
	var $detectLineSwaps = false;

	function diff($oldData, $newData) {
		$startOfDiffing = getmicrotime();
		
		// DO NOT UNDER ANY CIRCUMSTANCE TOUCH THIS LINE!!!!  EDITING
		// OR DELETING THIS LINE WILL BREAK THIS FUNCTION!
		define("strQUOTE", "\"");
		
		// First we check if the two datasets are the same by doing a direct
		// data comparison using PHP's own data comparison functions.  If
		// they are the same we simply return an empty Array.
		if ($oldData == $newData) return array();
		
		// Now we find out what kind of data we were fed, specifically if
		// we were fed an Array or not.  If we were fed a String value we
		// want to convert it to an Array before we go ahead and process.
		if (!is_array($oldData)) $oldData = explode("\n", $oldData);
		if (!is_array($newData)) $newData = explode("\n", $newData);
		
		// Checks to see if the necessary constant
		// exists, if not the function aborts.
		if (!defined("strQUOTE")) die("Necessary constant doesn't exist, function aborted!");
		
		// Sets up the internal change tracking system.
		$internalDifferences['removed'] = array();
		$internalDifferences['added'] = array();
		
		// The newData Array is larger than the oldData Array.  We assume
		// that something has been added to the new dataset.
		if (count($newData) > count($oldData)) {
			// We use PHP's in_array function to see if a line in the
			// oldData Array doesn't exist in the newData Array and
			// if it doesn't we assume that it was removed.
			foreach ($oldData as $line) {
				if (!in_array($line, $newData)) $internalDifferences['removed'][] = $line;
			}
			
			// We use PHP's in_array function to see if a line in the
			// newData Array doesn't exist in the oldData Array and
			// if it doesn't we assume that it was added.
			foreach ($newData as $line) {
				if (!in_array($line, $oldData)) $internalDifferences['added'][] = $line;
			}
		}
		// The newData Array is the same size as the oldData Array.
		// We assume that something has been edited or changed.
		elseif (count($newData) == count($oldData)) {
			// First we cycle through the oldData Array and do line-by-line
			// data comparison and keep track of the line numbers.  If the
			// data on a line in the newData Array doesn't match what's on
			// the same line in the oldData Array we assume that the line
			// has been removed.
			foreach ($oldData as $lineNumber => $line) {
				if ($newData[$lineNumber] != $oldData[$lineNumber]) $internalDifferences['removed'][] = $line;
			}
			
			// Second we cycle through the newData Array and do line-by-line
			// data comparison and keep track of the line numbers.  If the
			// data on a line in the newData Array doesn't match what's on
			// the same line in the oldData Array we assume that the line
			// has been added.
			foreach ($newData as $lineNumber => $line) {
				if ($newData[$lineNumber] != $oldData[$lineNumber]) $internalDifferences['added'][] = $line;
			}
		
			// This section checks to see if there were simple line swaps
			// with data in the two datasets.  This way you won't get a
			// a removal of a line only to have it added back in just
			// because it was at a different spot in the dataset.
			if ($this->detectLineSwaps) {
				foreach ($internalDifferences['added'] as $index => $line) {
					if (in_array($line, $internalDifferences['removed'])) {
						$key = array_search($line, $internalDifferences['added']);
						unset($internalDifferences['added'][$key]);
						
						$key = array_search($line, $internalDifferences['removed']);
						unset($internalDifferences['removed'][$key]);
					}
				}
				
				foreach ($internalDifferences['removed'] as $index => $line) {
					if (in_array($line, $internalDifferences['added'])) {
						$key = array_search($line, $internalDifferences['added']);
						unset($internalDifferences['added'][$key]);
						
						$key = array_search($line, $internalDifferences['removed']);
						unset($internalDifferences['removed'][$key]);
					}
				}
			}
		}
		elseif (count($newData) < count($oldData)) {
			// We use PHP's in_array function to see if a line in the
			// newData Array doesn't exist in the oldData Array and
			// if it doesn't we assume that it was added.
			foreach ($newData as $line) {
				if (!in_array($line, $oldData)) $internalDifferences['added'][] = $line;
			}
			
			// We use PHP's in_array function to see if a line in the
			// oldData Array doesn't exist in the newData Array and
			// if it doesn't we assume that it was removed.
			foreach ($oldData as $line) {
				if (!in_array($line, $newData)) $internalDifferences['removed'][] = $line;
			}
		}
		
		// Sets up the Array that we are going to be exporting out of this function.
		$differences = array();
		
		// Cycles through the $internalDifferences and adds the appropriate lines to
		// the $differences Array that we will be exporting out of this function.
		foreach ($internalDifferences['removed'] as $line) $differences[] = $this->strRemoved . strQUOTE . $line . strQUOTE;
		foreach ($internalDifferences['added'] as $line) $differences[] = $this->strAdded . strQUOTE . $line . strQUOTE;
		
		// Finally we return our differences Array for
		// later processing outside this function.
		return $differences;
	}
}

?>
Return current item: Very Simple Diff