Cradle  0.3.3
Simple library for creating Web-based applications
Diff Class Reference

Diff wrapper. More...

Static Public Member Functions

static diff ($text1, $text2, $bin=false, $options=NULL)
 
static diffParse ($text1, $text2)
 
static compare ($text1, $text2, $fill=true)
 

Detailed Description

Diff wrapper.

A simple wrapper of Linux diff utility to compare text data line by line.

This class just use Linux diff command.

Version
1.0
Author
Digger mrdig.nosp@m.ger@.nosp@m.sad-s.nosp@m.yste.nosp@m.ms.ru

Example of usage:

<?php
//--- Display the text returned by `diff` utility:
print_r( Diff::diff('some text1', 'some text2') );
//--- Display parsed `diff` results as an array:
print_r( Diff::diffParse('some text1', 'some text2') );
//--- Compare two text:
list($data1, $data2) = Diff::compare('some text1', 'some text2');
function map($data) {
return array_map(function($value){
list($state, $string) = $value;
return ($state ? $state : ' ') . ": " . $string;
}, $data);
}
echo "The first text data: " . print_r( map($data1), true ) . "\n";
echo "The second text data: " . print_r( map($data2), true ) . "\n";

Member Function Documentation

static compare (   $text1,
  $text2,
  $fill = true 
)
static

Compares two texts and represents results as array.

Parameters
string$text1Input text with an old data.
string$text2Input text with a new data.
boolean$fillTRUE by default. If this parameter is FALSE the difference between two text will be just marked. Otherwise the missing strings will be filled by emptiness.
Returns
array An array of structure:
array (
[0] => [ //--- The first text (from an old data):
0 => [ state, "text of string 0"],
1 => [ state, "text of string 1"],
...
],
[1] => [ //--- The second text (from a new data):
0 => [ state, "text of string 0"],
1 => [ state, "text of string 1"],
...
]
)
where `state` is one of:
'a' - added;
'd' - deleted;
'c' - changed;
'' - not changed;
'f' - filled (missing) string;
static diff (   $text1,
  $text2,
  $bin = false,
  $options = NULL 
)
static

Compare two text data line by line

Parameters
string$text1
string$text2
boolean$binTRUE - for binary files;
FALSE - for text files
string$options"-u" - Universal format
Returns
string A text data with diff utility results.
Exceptions
Exception
static diffParse (   $text1,
  $text2 
)
static

Parses diff results into the array.

Parameters
string$text1Input text with an old data.
string$text2Input text with a new data.
Returns
array An array of structure:
array (
[block_index] => [
0 => state, // state is: a - added, d - deleted, c - changed
1 => [start_indnex, end_indnex], // of text1 (old data)
2 => [start_indnex, end_indnex], // of text2 (new data)
3 => "raw diff string", // (option for debug or other parse)
]
)

The documentation for this class was generated from the following file: