public static function compare($string1, $string2, $compareCharacters = false, $ignoreIndent = true)
{
// initialise the sequences and comparison start and end positions
$start = 0;
if ($compareCharacters) {
$sequence1 = $string1;
$sequence2 = $string2;
$end1 = strlen($string1) - 1;
$end2 = strlen($string2) - 1;
} else {
$sequence1 = preg_split('/\\R/', $string1);
$sequence2 = preg_split('/\\R/', $string2);
$end1 = count($sequence1) - 1;
$end2 = count($sequence2) - 1;
}
// skip any common prefix
while ($start <= $end1 && $start <= $end2 && static::compareLine($sequence1[$start], $sequence2[$start], $ignoreIndent)) {
$start++;
}
// skip any common suffix
while ($end1 >= $start && $end2 >= $start && static::compareLine($sequence1[$end1], $sequence2[$end2], $ignoreIndent)) {
$end1--;
$end2--;
}
// compute the table of longest common subsequence lengths
$table = self::computeTable($sequence1, $sequence2, $start, $end1, $end2, $ignoreIndent);
// generate the partial diff
$partialDiff = self::generatePartialDiff($table, $sequence1, $sequence2, $start, $ignoreIndent);
// generate the full diff
$diff = array();
for ($index = 0; $index < $start; $index++) {
$diff[] = array($sequence1[$index], self::UNMODIFIED);
}
while (count($partialDiff) > 0) {
$diff[] = array_pop($partialDiff);
}
for ($index = $end1 + 1; $index < ($compareCharacters ? strlen($sequence1) : count($sequence1)); $index++) {
$diff[] = array($sequence1[$index], self::UNMODIFIED);
}
// return the diff
return $diff;
}