Author: Manuel Lemos
Posted on: 2014-01-31
Package: PHP Text Diff Highlight class
Now these classes can also take the initial string and rebuild the final string applying the previously extracted list of differences.
Read this article to learn how to use these classes to show the diff between strings and apply the list of differences patch.
Contents
Showing the Differences between Strings on a Web Page
Comparing strings by Character, Word or Line
Getting the Final String by Patching the Original String
Showing the Diff and Patching Text in JavaScript
Conclusion
Showing the Differences between Strings on a Web Page
The PHP Text Diff Highlight class is very straightforward. Once you create an object of the class, just call diff function passing as parameters the initial text, the final text text, and a reference to an object that passes parameter values and returns the results of the diff operation.
$difference = new stdClass; $difference->mode = 'w'; $before = 'Some text before'; $after = 'This is the text after'; $diff = new diff_class; if(!$diff->diff($before, $after, $difference)) { die($diff->error); } else { echo 'Changes: '.print_r($difference->differences, 1); }
The class can also show the differences between the two strings by using the function formatDiffAsHtml instead of the diff function. It builds a HTML string with the added text marked using underline and bold style and the removed text in strike-through style. You can configure the styles for presenting the added and the removed text.
The formatted changes HTML may be used to display the differences to the user in a Web page.
if($diff->formatDiffAsHtml($before, $after, $difference)) { echo '<div>', $difference->html, '</div>'; }
The differences between the strings will show like this:
Some This is the text beforeafter
Comparing strings by Character, Word or Line
The comparison between the two text strings can be done in three modes: by line or by word for regular text, or by character which is more suitable for comparing binary data strings.
The comparison mode is configured by setting the difference parameter of the diff or formatDiffAsHtml functions. The mode option values are 'c' for character, 'w' for word and 'l' for line.
Getting the Final String by Patching the Original String
The list of changes between the original string and the final string is returned as an array in the differences variable $difference parameter object. This array can be used to rebuild the final string by calling the patch function, passing the initial string as parameter.
You need to set the patch variable of the $difference parameter object to true to make the class return all the data that the patch function needs to rebuild the final string.
The patch function returns the patched string as the after variable of the afterPatch parameter object.
$difference = new stdClass; $difference->mode = 'w'; $difference->patch = true; $afterPatch = new stdClass; if($diff->diff($before, $after, $difference) && $diff->patch($before, $difference->differences, $afterPatch)) { echo 'The before text patched with the differences is: '. $afterPatch->after; }
Showing the Diff and Patching Text in JavaScript
If you need to see the differences between the original text and a text being edited in a form, you need the JavaScript counterpart of the PHP class version, which is the JS Diff Viewer object.
That JavaScript object provides the exact same functionality of finding the differences between text strings, displaying the differences formatted as HTML and patching the original text to rebuild the final text using the list of differences.
There is another article similar to this to explain how to use that JavaScript object for viewing the differences and patch text strings in JavaScript.
Conclusion
This PHP class and the JavaScript object are very useful for the viewing the differences between two strings on a Web page. If you have other needs that you would like these classes to address or have any other question, just put a comment here to this article.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
Comments:
No comments were submitted yet.