Diff::compare PHP Method

compare() public static method

$string1 - the first string $string2 - the second string $compareCharacters - true to compare characters, and false to compare lines; this optional parameter defaults to false $ignoreIndent - false to compare also white spaces in the beginning on the line, true to ignore them
public static compare ( $string1, $string2, $compareCharacters = false, $ignoreIndent = true )
    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;
    }

Usage Example

コード例 #1
0
ファイル: DiffTest.php プロジェクト: nathanieltite/elefant
 function test_format()
 {
     $diff = new Diff(DIFF_SPACE);
     $one = 'a b c d e f g';
     $two = 'a c d e t t f';
     $res = $diff->compare($one, $two);
     $out = $diff->format($res);
     $expected = array(array('', 'a'), array('-', 'b'), array('', 'c'), array('', 'd'), array('', 'e'), array('+', 't'), array('+', 't'), array('', 'f'), array('-', 'g'));
     $this->assertEquals($out, $expected);
 }
All Usage Examples Of Diff::compare