class Diff (View source)

A class containing functions for computing diffs and formatting the output.

We can compare 2 strings or 2 files (as one string or line by line)

Constants

UNMODIFIED

DELETED

INSERTED

Methods

static array<array{0: string, 1: int<0, 2>}>
compare(string $string1, string $string2, bool $compareCharacters = false)

Returns the diff for two strings. The return value is an array, each of whose values is an array containing two values: a line (or character, if $compareCharacters is true), and one of the constants DIFF::UNMODIFIED (the line or character is in both strings), DIFF::DELETED (the line or character is only in the first string), and DIFF::INSERTED (the line or character is only in the second string). The parameters are:

static array<array{0: string, 1: int<0, 2>}>
compareFiles(string $file1, string $file2, bool $compareCharacters = false)

Returns the diff for two files. The parameters are:

static string
toString(array<array{0: string, 1: int<0, 2>}> $diff, string $separator = "\n")

Returns a diff as a string, where unmodified lines are prefixed by ' ', deletions are prefixed by '- ', and insertions are prefixed by '+ '. The parameters are:

static string
toHTML(array<array{0: string, 1: int<0, 2>}> $diff, string $separator = '<br>')

Returns a diff as an HTML string, where unmodified lines are contained within 'span' elements, deletions are contained within 'del' elements, and insertions are contained within 'ins' elements. The parameters are:

static string
toTable(array<array{0: string, 1: int<0, 2>}> $diff, string $indentation = '', string $separator = '<br>')

Returns a diff as an HTML table. The parameters are:

Details

static array<array{0: string, 1: int<0, 2>}> compare(string $string1, string $string2, bool $compareCharacters = false)

Returns the diff for two strings. The return value is an array, each of whose values is an array containing two values: a line (or character, if $compareCharacters is true), and one of the constants DIFF::UNMODIFIED (the line or character is in both strings), DIFF::DELETED (the line or character is only in the first string), and DIFF::INSERTED (the line or character is only in the second string). The parameters are:

Parameters

string $string1

First string

string $string2

Second string

bool $compareCharacters

true to compare characters, and false to compare lines; this optional parameter defaults to false

Return Value

array<array{0: string, 1: int<0, 2>}>

Array of diff

static array<array{0: string, 1: int<0, 2>}> compareFiles(string $file1, string $file2, bool $compareCharacters = false)

Returns the diff for two files. The parameters are:

Parameters

string $file1

Path to the first file

string $file2

Path to the second file

bool $compareCharacters

true to compare characters, and false to compare lines; this optional parameter defaults to false

Return Value

array<array{0: string, 1: int<0, 2>}>

Array of diff

static string toString(array<array{0: string, 1: int<0, 2>}> $diff, string $separator = "\n")

Returns a diff as a string, where unmodified lines are prefixed by ' ', deletions are prefixed by '- ', and insertions are prefixed by '+ '. The parameters are:

Parameters

array<array{0: string, 1: int<0, 2>}> $diff

the diff array

string $separator

the separator between lines; this optional parameter defaults to "\n"

Return Value

string String

static string toHTML(array<array{0: string, 1: int<0, 2>}> $diff, string $separator = '<br>')

Returns a diff as an HTML string, where unmodified lines are contained within 'span' elements, deletions are contained within 'del' elements, and insertions are contained within 'ins' elements. The parameters are:

Parameters

array<array{0: string, 1: int<0, 2>}> $diff

the diff array

string $separator

the separator between lines; this optional parameter defaults to '
'

Return Value

string

HTML string

static string toTable(array<array{0: string, 1: int<0, 2>}> $diff, string $indentation = '', string $separator = '<br>')

Returns a diff as an HTML table. The parameters are:

Parameters

array<array{0: string, 1: int<0, 2>}> $diff

the diff array

string $indentation

indentation to add to every line of the generated HTML; this optional parameter defaults to ''

string $separator

the separator between lines; this optional parameter defaults to '
'

Return Value

string

HTML string