PHP Classes

Patch the text back

Recommend this page to a friend!

      PHP Text Diff Highlight class  >  All threads  >  Patch the text back  >  (Un) Subscribe thread alerts  
Subject:Patch the text back
Summary:Patch the text back
Messages:5
Author:Melanie Wehowski
Date:2013-12-20 20:16:13
Update:2014-01-29 09:58:47
 

  1. Patch the text back   Reply   Report abuse  
Picture of Melanie Wehowski Melanie Wehowski - 2013-12-20 20:16:13
Hello Manuel,
I added
if($pb !== $b)
{
$diff[] = array(
'change'=>'-',
'position'=>($mode === 'c' ? $b : $posb[$b]),
'length'=>($mode === 'c' ? $pb - $b : $posb[$pb] - $posb[$b]),
'string' => substr($before, ($mode === 'c' ? $b : $posb[$b]), ($mode === 'c' ? $pb - $b : $posb[$pb] - $posb[$b]) ),
);
$b = $pb;
}
if($pa !== $a)
{
$diff[] = array(
'change'=>'+',
'position'=>($mode === 'c' ? $a : $posa[$a]),
'length'=>($mode === 'c' ? $pa - $a : $posa[$pa] - $posa[$a]),
'string' => substr($after, ($mode === 'c' ? $a : $posa[$a]), ($mode === 'c' ? $pa - $a : $posa[$pa] - $posa[$a]) ),
);
$a = $pa;
}
}
if($a < $la)
{
$diff[] = array(
'change'=>'+',
'position'=>($mode === 'c' ? $a : $posa[$a]),
'length'=>($mode === 'c' ? $la - $a : $posa[$la] - $posa[$a]),
'string' => substr($after, ($mode === 'c' ? $a : $posa[$a]), ($mode === 'c' ? $pa - $a : $posa[$pa] - $posa[$a]) ),
);
}


I try to patch the text back the the original by just storing the text $after and the $diff array reverse, but I'm not satisfied by my solutions, maybe you have a hint or ready solution...?

regards,
Till

  2. Re: Patch the text back   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-12-23 16:57:06 - In reply to message 1 from Melanie Wehowski
I am not sure what you mean. Do you want to patch the original string to get the final string using the difference data?

Basically patching and showing the differences is almost the same thing. It is just a slightly different way to using the difference data.

I can add a function to patch the original string with the difference data if that is helpful, but it will just be a variant of the difference highlight function.

  3. Re: Patch the text back   Reply   Report abuse  
Picture of Melanie Wehowski Melanie Wehowski - 2013-12-23 17:19:18 - In reply to message 2 from Manuel Lemos
Yes, that is what I like to do.

The html method expects $before, $after and $diff as parameter.
I would like to patch back from just knowing. $after and $diff.
To do so, I believe (? ) I should store the added and deleted strings ind $diff, aswell as the positions of the equals in $after, am I right?

I found something on php.net http://www.php.net/manual/en/function.levenshtein.php from bisqwit at iki dot fi I used before in one project, I believed I could use your script to improve, that I do not have to save the before text and to not save the equals to patch the text back to the original version...?

greetings,
till

  4. Re: Patch the text back   Reply   Report abuse  
Picture of Melanie Wehowski Melanie Wehowski - 2013-12-31 03:52:21 - In reply to message 3 from Melanie Wehowski
This code works as expected in the first test but maybe can be optimized?

I tested with the function from php.net but your version may work the same way.
I this example I saved the stringlength in $d[2].

This should patch the text back to the previous version by just knowing/storing
- the new string
- the deleted strings
- the stringlength of the added and equal strings

To patch back a long history of changes could take time this way but you can save a lot of diskspace e.g. if the editor just changed a few characters of a huge text for example.

$back = array_reverse($diff);
$str = '';
$r = strrev($newversion);
$offset = 0;
foreach($back as $step => $d)
{
$d[1] = strrev($d[1]);
if($d[0] === '=')$str.= substr($r, $offset, $d[2] );
if($d[0] === '-')$str.= $d[1];
if($d[0] === '+' || $d[0] === '=')$offset += $d[2];

}
$oldversion = strrev($str);

  5. Re: Patch the text back   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-01-29 09:58:47 - In reply to message 4 from Melanie Wehowski
Sorry for the delay. Only now I made time to look into this.

The class is a bit more complex than that, as it also can compare texts by word and by line.

I just added a new function named Patch that takes the string before and the difference array and rebuilds the after string.

I am writing the JavaScript counterpart and plan to write an article about this. Hang on.