PHP Classes

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

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 229 All time: 8,171 This week: 86Up
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: 107269 in Germany Germany
Week rank: 49 Up3 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 (2)  
File Role Description
Accessible without login Plain text file Compare.php Class Class source
Accessible without login Plain text file example.php Example Example

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 50%
Total:229
This week:0
All time:8,171
This week:86Up