Reputation: 43884
First off, this is an exact duplicate of these four questions:
It seems as though times have changed since these questions were first asked and I am wondering what is a good tool now-a-days for this sort of comparison? I have looked at (additionally to those questions):
But all of the ones I get are either unmantained now or seem a little dodgy in that they are not used that much (and some even hint that they are not very performant) and the PEAR one worries me. I hate to install PEAR for one little module not only that but it seems like throwing a brick through my own window to install it for such a small module in comparison to PEAR in general not only that but the module has been superseded and placed on a different channel (dunno why?). I would use the PEAR version if it is my only choice but I want to use the upto date package.
Does anyone know of a well used and currently maintained or built in function (even if it is a PHP extension) text diff for PHP and/or JavaScript (JQuery as well)?
Upvotes: 10
Views: 1615
Reputation: 43884
Ok so it has been a while.
I actually decided to look around at what other people use and stumbled upon what Yii ( http://www.yiiframework.com ) uses.
They actually use the PEAR module for their text_diff
and they use it in its new form on the horde channel. It seems that text_diff
is now a horde project but you can just as easily integrate a version of it into your application and that is what Yii does by default (it comes pre-bundled with a version of it).
So I searched around a bit to find out how they used it and how to get into it and I came across:
public function actionDiff()
{
Yii::import('gii.components.TextDiff');
$model=$this->prepare();
if(isset($_GET['id']) && isset($model->files[$_GET['id']]))
{
$file=$model->files[$_GET['id']];
if(!in_array($file->type,array('php', 'txt','js','css')))
$diff=false;
elseif($file->operation===CCodeFile::OP_OVERWRITE)
$diff=TextDiff::compare(file_get_contents($file->path), $file->content);
else
$diff='';
$this->renderPartial('/common/diff',array(
'file'=>$file,
'diff'=>$diff,
));
}
else
throw new CHttpException(404,'Unable to find the code you requested.');
}
In CCodeGenerator
for their Gii module ( http://www.yiiframework.com/doc/api/1.1/CCodeGenerator/ ). The important part is where they actually hook into the PEAR module:
$diff=TextDiff::compare(file_get_contents($file->path), $file->content);
By reading in the contents of two files which produces a diffed
output.
Originally I did not want to use PEAR because of the bloat but this module is quite slim for a fully featured text_diff
so I have decided to go with this. Not only that but, at the moment, it is the only text_diff
module that has truly worked for me so I am keeping with the best, even if the best is quite memory hungry.
Upvotes: 2
Reputation: 1533
Have you tried one of Philippe's two solutions on this thread?
Quoted here:
In PHP. array_diff compares the first against the second array and returns the difference.
$a1 = str_split('abcdefghijklmnop'); $a2 = str_split('abcdefghi'); echo join('', array_diff($a1, $a2)); // jklmnop
This will work as well:
$s1 = 'abcdefghijklmnop'; $s2 = 'abcdefghi'; echo str_replace(str_split($s2), '', $s1); // jklmnop
This could handle
$s2 = 'ghiabcdef';
as well becausestr_replace()
is fed with an array, not a string.
Upvotes: 1