PHP Classes

PHP Text Diff Highlight class: Find and view the difference between text strings

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum (5)   Blog (1)    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 62%Total: 1,735 All time: 2,262 This week: 46Up
Version License PHP version Categories
php-diff 1.0.2BSD License3.0HTML, Text processing


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: 55
All time rank: 1
Week rank: 6 Down1 in Portugal Portugal Equal



 * 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>';


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


$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  
File Role Description
Plain text file diff.php Class Diff PHP class
Accessible without login Plain text file Doc. Basic instructions
Accessible without login Plain text file test_diff.php Example Example page to show the difference between two texts

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:2,262
This week:46Up
User Ratings User Comments (2)
 All time
Awesome job and awesome package this is exactly the same whic...
7 years ago (khalid)
Awesome job
9 years ago (Patrick Mcguire)