PHP Classes

PHP Diff Text Strings: Find differences between texts and patch strings

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 226 All time: 8,179 This week: 136Up
Version License PHP version Categories
text-diff-rollback 4.0.2Public Domain5.3Algorithms, PHP 5, Text processing
Description 

Author

This class can find differences between texts and patch strings.

It can compare two given text strings and return the differences between them.

The class can also patch the original string using an optimized version of the difference information to obtain the second string.

Picture of Till Wehowski
  Performance   Level  
Name: Till Wehowski <contact>
Classes: 30 packages by
Country: Germany Germany
Age: 45
All time rank: 107669 in Germany Germany
Week rank: 332 Up10 in Germany Germany Up
Innovation award
Innovation award
Nominee: 12x

Recommendations

Example

<?php

$text_1
= 'Dies ist ein Test uber Hackfleisch.<br />Dies ist ein Text uber Hackfleisch.';
$text_2 = "Dies ist Text uber Kackfleisch.<br />Dies ist Test uber Kackfleisch.";

$newversion = $text_2;

$comp = new \webdof\Text\diff\Compare();
$diff = $comp->calcdiffer($text_1, $text_2);
$html.= $text_1.'<br />';
$html.= '<hr noshade>';
$html.= $text_2.'<br />';
$html.= '<pre>'.print_r($diff, true).'</pre>';

$back = '';
foreach(
$diff as $step => $d)
 {
   if(
$d[0] === '=' || $d[0] === '-')$back.= $d[1];
 }

$html.= 'Patching:<br />';
$html.= $back.'<br />';


$html.= '<br />';
$html.= 'Patching R&uuml;w&auml;rts nur mit &Auml;nderungen und strlen(equals):<br />';

$diff = $comp->minimize( $diff );
$oldversion = $comp->patch( $text_2, $diff );

$html.= '<pre>'.print_r($diff, true).'</pre>';
$html.= $oldversion.'<br />';

$html.= '<br /><br />Test 2<br />';
$text_1 = 'An einem Sommertag fuhr ich mit dem Fahrrad über die Straße nach Hause.';
$text_2 = 'An einem schönen Sommertag ging ich mit dem Auto über die Straße spazieren.';

$html.= $text_1.'<br />';
$html.= $text_2.'<br />';

$comp = new \webdof\Text\diff\Compare();
$diff = $comp->calcdiffer($text_1, $text_2);
$diff = $comp->minimize( $diff );
$oldversion = $comp->patch( $text_2, $diff );
$html.= $oldversion .'<br />';


echo
$html;


  Files folder image Files  
File Role Description
Accessible without login Plain text file Compare.php Class Class source
Accessible without login Plain text file example.php Example Example

 Version Control Unique User Downloads Download Rankings  
 50%
Total:226
This week:0
All time:8,179
This week:136Up