PHP Classes

PHP Text Diff Highlight class: Find and view the difference between text 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
StarStarStarStar 62%Total: 1,740 All time: 2,258 This week: 48Up
Version License PHP version Categories
php-diff 1.0.2BSD License3.0HTML, Text processing
Description 

Author

This class can find and view the difference between text strings.

It takes two text strings and uses the diff algorithm to find the differences between them and return a list of changes to patch the original string to become the final string.

The patch list shows what text should be added or removed to change one string into the other.

The difference between the text strings may be computed in three modes: by character, by word or by line.

The class may also format the strings to view them as HTML showing which characters are added and removed with special insertion and deleted styles.

The example page works as a tool to interactively view the changes as the user changes the texts before and after the changes are applied.

The class can also patch the original string using the patch list to regenerated the changed string.

Picture of Manuel Lemos
  Performance   Level  
Name: Manuel Lemos is available for providing paid consulting. Contact Manuel Lemos .
Classes: 45 packages by
Country: Portugal Portugal
Age: 56
All time rank: 1
Week rank: 5 Down2 in Portugal Portugal Down

Recommendations

Example

<?php
/*
 * test_diff.php
 *
 * @(#) $Id: test_diff.php,v 1.6 2014/01/30 04:07:41 mlemos Exp $
 *
 */
   
require('diff.php');
 
 
$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">
<head>
<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; }
</style>
<body>
<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>
</select></div>
<?php
   
if(IsSet($_POST['compare']))
    {
       
$diff = new diff_class;
       
$difference = new stdClass;
       
$difference->mode = $mode;
       
$difference->patch = true;
       
$after_patch = new stdClass;
        if(
$diff->FormatDiffAsHtml($before, $after, $difference)
        &&
$diff->Patch($before, $difference->difference, $after_patch))
        {
            echo
'<div>Difference</div><div class="frameResults">', $difference->html, '</div>';
            echo
'<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>';
        }
        else
            echo
'<div>Error: ', HtmlSpecialChars($diff->error), '</div>';
    }
?>
</form>
</body>
</html>


Details

PHP Diff

Diff is the abbreviation of difference. It is also the name of Unix/Linux utility program that is able to compare two text files and shows the lines that differ between them.

This class implements the same functionality of the diff commmand, except that it is written in pure PHP, i.e. it does not require any external programs or other PHP classes or scripts.

Text Difference

You can compute the difference between two strings either line by line, word by word or character by character.

In the case of finding differences between words or lines, you would compare arrays of strings with the words or lines of the text.

PHP Find Difference Between Two Strings

require('diff.php');

$before = 'Some text before';
$after = 'This is the text after';

You can set the text mode option depending on whether you want to compare text character by character ('c'), word by word ('w') or line by line ('l').


$mode = 'w';

$diff = new diff_class;

The class returns a difference object that contains the list of differences between the two strings, as well may return a list of patch operations to transform one string in to the other.


if($diff->Diff($before, $after, &$difference))
  die('Diff error: '.$diff->error;);
  

PHP Text Diff Highlight

If you want to highlight the differences between two strings in a Web page you can use the FormatDiffAsHtml function instead, so it returns the first string with <ins> and <del> tags showing what excepts of the string should be removed or added to turn it into the second string.

if($diff->FormatDiffAsHtml($before, $after, $difference)
    die('Diff error: '.$diff->error;);
echo '<div>', $difference->html, '</div>';

  Files folder image Files (3)  
File Role Description
Plain text file diff.php Class Diff PHP class
Accessible without login Plain text file README.md Doc. Basic instructions
Accessible without login Plain text file test_diff.php Example Example page to show the difference between two texts

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  
 100%
Total:1,740
This week:0
All time:2,258
This week:48Up
User Ratings User Comments (2)
 All time
Utility:87%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:-
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:62%StarStarStarStar
Rank:973
 
Awesome job and awesome package this is exactly the same whic...
8 years ago (khalid)
70%StarStarStarStar
Awesome job
9 years ago (Patrick Mcguire)
70%StarStarStarStar