PHP Classes

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

Recommend this page to a friend!

  Author Author  
Picture of Manuel Lemos
Name: Manuel Lemos is available for providing paid consulting. Contact Manuel Lemos .
Classes: 43 packages by
Country: Portugal Portugal
Age: 54
All time rank: 1
Week rank: 1 Equal

  Detailed description   Download Download .zip .tar.gz   Install with Composer Install with Composer  
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.


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

  Classes of Manuel Lemos  >  PHP Text Diff Highlight class  >  Download Download .zip .tar.gz  >  Support forum Support forum (5)  >  Blog Blog (1)  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP Text Diff Highlight class
Base name: php-diff
Description: Find and view the difference between text strings
Version: 1.0.2
PHP version: 3.0
License: BSD License
All time users: 1646 users
All time rank: 2376
Week users: 2 users
Week rank: 349 Up
  Groups   Rate classes User ratings   Applications   Files Files  

Group folder image HTML HTML generation and processing View top rated classes
Group folder image Text processing Manipulating and validating text data View top rated classes


What is the best PHP class to compare strings?
Compare strings

  User ratings  
RatingsUtility Consistency Documentation Examples Tests Videos Overall Rank
All time: Good (95%) Perfect (100%) - Perfect (100%) - - Sufficient (67%) 615
Month: Not yet rated by the users

  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  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

Install with Composer Install with Composer - Download Download all files: php-diff.tar.gz
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.