PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Till Wehowski   PHP Diff Text Strings   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example
Class: PHP Diff Text Strings
Find differences between texts and patch strings
Author: By
Last change:
Date: 10 years ago
Size: 1,367 bytes
 

Contents

Class file image Download
<?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;