PHP Classes

File: test_diff.php

Recommend this page to a friend!
  Classes of Manuel Lemos   PHP Text Diff Highlight class   test_diff.php   Download  
File: test_diff.php
Role: Example script
Content type: text/plain
Description: Example page to show the difference between two texts
Class: PHP Text Diff Highlight class
Find and view the difference between text strings
Author: By
Last change: Added support to patch the text before and obtain the text after using only
the list of differences.
Added the Patch function to patch the string before to recreate the string
Date: 10 years ago
Size: 2,169 bytes


Class file image Download
 * test_diff.php
 * @(#) $Id: test_diff.php,v 1.6 2014/01/30 04:07:41 mlemos Exp $
$before = IsSet($_POST['before']) ? $_POST['before'] : 'Some text before';
$after = IsSet($_POST['after']) ? $_POST['after'] : 'This is the text after';
$mode = (IsSet($_POST['mode']) ? $_POST['mode'] : 'w');
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<title>Test the Diff Object</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
* { font-family: sans-serif,arial,helvetica }
.frameResults { border-style: solid; border-width: 1px; }
<form method="POST" action="?">
<div><label for="before">Before</label><br>
<textarea id="before" cols="80" rows="10" name="before"><?php echo HtmlSpecialChars($before); ?></textarea></div>
<div><label for="after">After</label><br>
<textarea id="after" cols="80" rows="10" name="after"><?php echo HtmlSpecialChars($after); ?></textarea></div>
<div><input type="submit" name="compare" value="Compare"> by <select name="mode">
<option value="c"<?php if($mode === 'c') echo ' selected'; ?>>Character</option>
<option value="w"<?php if($mode === 'w') echo ' selected'; ?>>Word</option>
<option value="l"<?php if($mode === 'l') echo ' selected'; ?>>Line</option>
$diff = new diff_class;
$difference = new stdClass;
$difference->mode = $mode;
$difference->patch = true;
$after_patch = new stdClass;
$diff->FormatDiffAsHtml($before, $after, $difference)
$diff->Patch($before, $difference->difference, $after_patch))
'<div>Difference</div><div class="frameResults">', $difference->html, '</div>';
'<div>Patch</div><div class="frameResults">', ($after === $after_patch->after ? 'OK: The patched text matches the text after.' : 'There is a BUG: The patched text (<b>'.HtmlSpecialChars($after_patch->after).'</b>) does not match the text after (<b>'.HtmlSpecialChars($after).'</b>).'), '</div>';
'<div>Error: ', HtmlSpecialChars($diff->error), '</div>';