2

I am aware that there are many text diff for PHP, but I haven't been able to find one that returns the positions insertions where made at.

E.g.

Before: This is a apple

After: This is an apple

'Insertion of a at 8'

.* I think it's 8 *

So, this diff should occur at the character level. I should be able to, from the returns reproduce the 'After' text from the 'Before' text.

Any thoughts? Thanks in advance,

ProgrammerMan
  • 431
  • 3
  • 7
  • Possible duplicate 1. [Calculate text diffs in PHP](http://stackoverflow.com/questions/112523/calculate-text-diffs-in-php) 2. [Highlight the difference between two strings in PHP](http://stackoverflow.com/questions/321294/highlight-the-difference-between-two-strings-in-php) – dan-lee Mar 24 '12 at 03:37
  • Not a duplicate, because the answers for these only present a solution for obtaining the diff's, but not exactly return the positions of the insertions/deletions so that one may use them to completely replicate the new text from the old text, as I outline above. – ProgrammerMan Mar 24 '12 at 14:43

1 Answers1

0

I spent some time and arrived at a starting point for you:

function string_compare($string1, $string2) {
        $chars1 = str_split($string1);
        $chars2 = str_split($string2);

        $longer = strlen($string1) > strlen($string2) ? $chars1 : $chars2;
        $shorter = strlen($string1) > strlen($string2) ? $chars2 : $chars1;

        $key_offset = 0;
        $changes = array();

        foreach($longer as $key => $char) {
            #$key2 = $key + $key_offset;
            $char2 = isset($shorter[$key]) ? $shorter[$key] : '';

            if($char !== $char2) {
                if(empty($char2)) {
                    $changes[$key] = array('remove' => $char);
                } elseif(empty($char)) {
                    $changes[$key] = array('add' => $char);
                } else {
                    $changes[$key] = array('change' => $char.' to '.$char2);
                }
            }
        }

        return $changes;
    }

The only drawback, is that it returns the changes on how to get the longer string into the shorter one.


This function chokes, don't use it, sorry.

AVProgrammer
  • 1,344
  • 2
  • 20
  • 40