Class: Diff

  • Package: saf.Diff
  • Author: John Luxford <lux@simian.ca>
  • Copyright: Copyright (C) 2001-2003, Simian Systems Inc.
  • License: http://www.sitellite.org/index/license Simian Open Software License
  • Version: 1.0, 2002-11-19, $Id: Diff.php,v 1.3 2007/10/06 00:06:30 lux Exp $
  • Access: public

Diff provides a lightweight (fast), simple, and effective means of
comparing the difference between two strings. Diff bears no code-level
similarity to most of the popular diff algorithm implementations, because
it relies on three simple, built-in PHP functions to accomplish the tough
stuff, being preg_split(), array_diff(), and array_intersect(). This
should make the SAF Diff package much faster than most others.


Usage Example


<?php

$diff 
= new Diff (DIFF_SPACE);

$original 'a b c e h j l m n p';
$new 'b c d e f j k l m r s t';

echo 
'Comparison: ' $original ' : ' $new '<br />';
foreach (
$diff->format ($diff->compare ($original$new)) as $line) {
    if (
$line[0]) {
        echo 
$line[0] . '&nbsp;' htmlentities_compat ($line[1]) . '<br />';
    } else {
        echo 
'&nbsp;&nbsp;' htmlentities_compat ($line[1]) . '<br />';
    }
}

?>

Return to Top



Properties


$splitMode

  • Access: public

Contains the regular expression to use to split the
original strings into arrays for comparison via the array_diff()
and array_intersect() functions. The two preset modes are
defined in the constants DIFF_LINE and DIFF_SPACE, which split
by newline character and by blank space, respectively.

Return to Top



Methods


Diff ($splitMode = DIFF_LINE)

  • Access: public

Constructor method.


compare ($str1, $str2)

  • Access: public
  • Return: array

Compares two strings and returns a 2-D array of
the strings added, removed, and that are shared between
the two original strings.


format ($a, $r = false, $i = false)

  • Access: public
  • Return: array

Accepts the input from compare() either directly or
indirectly, and returns another 2-D array where each element
in the top level array is an array with the first value being
either false, "+" (plus), or "-" (minus), to represent whether
that line should be added or removed from the first original
string to produce the second, and the second value being the
string to add, remove, or keep as-is. The parameters $a,
$r, and $i stand for "add", "remove", and "intersect".

Return to Top

Copyright © 2008 Sitellite CMS Project

Powered by Sitellite 5.0 Content Management System