Is it possible to highlight the modifications in one text paragraph from the other?
For example, there are 3 text fields in a database. Non-admin users can edit the text and submit for approval. When the admin logs in, (s)he can open the approvals page and it shows the original text and user submitted text with modifications. Usually these modifications are very small in nature, like spelling correction or just a deletion or addition of a sentence.
Can it be presented in such a way that the modifications (from the original text) are highlighted so that admin users don't have to check every field and every line before they approve it?
Any help would be appreciated. Thanks.
Updated: Somewhat similar to TortoiseSVN's code changes highlighting.
Is it possible to highlight the modifications in one text paragraph from the other?
For example, there are 3 text fields in a database. Non-admin users can edit the text and submit for approval. When the admin logs in, (s)he can open the approvals page and it shows the original text and user submitted text with modifications. Usually these modifications are very small in nature, like spelling correction or just a deletion or addition of a sentence.
Can it be presented in such a way that the modifications (from the original text) are highlighted so that admin users don't have to check every field and every line before they approve it?
Any help would be appreciated. Thanks.
Updated: Somewhat similar to TortoiseSVN's code changes highlighting.
Share Improve this question edited Jul 14, 2009 at 14:03 Svante 51.5k11 gold badges83 silver badges125 bronze badges asked Jul 14, 2009 at 13:04 TigerTigerTigerTiger 10.8k16 gold badges58 silver badges72 bronze badges4 Answers
Reset to default 11Options:
- PHP Inline Diff (uses PEAR text-diff)
- PEAR Text_Diff
- Diff in Pure PHP
- xdiff_string_diff()
Stackoverflow Archive:
- Highlight the difference between two strings in PHP
- Apply Diff in PHP
- Need Help Optimizing php string difference function
If you are familiar with GNU/Linux tools, you might be familiar with the `diff' tool.
And someone has written a fully functional diff for comparing files/strings in php.
You can find it here: php diff example.
This is an old thread but thought I'd catalog my findings.
If you want to use raw php, this is by far the simplest, fastest, and most effective solution I've found: http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/
From what I can tell, the author of the above code is using an algorithm outlined in this article: http://www.codeproject.com/Articles/6943/A-Generic-Reusable-Diff-Algorithm-in-C-II
Basic description of what's happening (taken from the article):
- Find the current Longest Matching Sequence (LMS) of items.
- Store this LMS in a results pile.
- Process all data left above the LMS using recursion.
- Process all data left below the LMS using recursion.
Works like a charm!
Not sure, why these long solutions are there. here I had found an easy one for me.
string1 = "The quick brown fox jumps over the lazy dog.";
$string2 = "The quick brown albino fox jumps the groovy dog.";
$string1 = explode(" ", $string1);
$string2 = explode(" ", $string2);
$diff = array_intersect($string2, $string1);
$tmp = array();
foreach ($string2 as $k => $w) {
if ($diff[$k]==$w) {
$tmp[$k] = $w;
}
else {
$tmp[$k] = "<b>$w</b>";
}
}
$diff = array_diff($string1, $tmp);
foreach ($diff as $k => $w) {
$tmp[$k] .= " [<strike>$w</strike>]";
}
echo join (' ', $tmp);
ref. https://forums.phpfreaks.com/topic/6525-how-do-i-highlight-differences-between-strings/