147

What is the easiest way to highlight the difference between two strings in PHP?

I'm thinking along the lines of the Stack Overflow edit history page, where new text is in green and removed text is in red. If there are any pre-written functions or classes available, that would be ideal.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Philip Morton
  • 129,733
  • 38
  • 88
  • 97

17 Answers17

81

Just wrote a class to compute smallest (not to be taken literally) number of edits to transform one string into another string:

http://www.raymondhill.net/finediff/

It has a static function to render a HTML version of the diff.

It's a first version, and likely to be improved, but it works just fine as of now, so I am throwing it out there in case someone needs to generate a compact diff efficiently, like I needed.

Edit: It's on Github now: https://github.com/gorhill/PHP-FineDiff

R. Hill
  • 3,582
  • 1
  • 20
  • 19
  • 3
    I'll try the fork at https://github.com/xrstf/PHP-FineDiff to get multibyte support! – activout.se Feb 04 '12 at 12:19
  • 1
    @R. Hill - Works beautifully for me too. This really is a better answer than the current one which appears to be defunct. – Wonko the Sane Jun 14 '15 at 22:51
  • Any updates? It says failed to include file "Texts/Diff.php" and it is not in the zip. – SISYN Jun 09 '16 at 14:59
  • Amazing! I mean the online demo with example code. Perfect char-level differences. Just WoW! :O Thank You! – Filip OvertoneSinger Rydlo Sep 22 '16 at 12:38
  • 3
    It seems that now the https://github.com/BillyNate/PHP-FineDiff fork is the most ahead and it supports multibytes with different encodings. https://github.com/xrstf/PHP-FineDiff is 404ing @activout.se – Kangur May 16 '18 at 22:23
46

You were able to use the PHP Horde_Text_Diff package.

However this package is no longer available.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
M.N
  • 10,899
  • 13
  • 47
  • 49
  • 1
    the link doesn't work anymore. is it any other solution now in 2011? ;-) is it possible go get output like this http://tortoisesvn.tigris.org/images/TMerge2Diff.png – Glavić Jan 05 '11 at 11:28
  • 3
    Site is gone, but archive.org has a copy of the site: http://web.archive.org/web/20080506155528/http://software.zuavra.net/inline-diff/ – R. Hill Jan 25 '11 at 13:49
  • 17
    Too bad it requires PEAR. PEAR-dependance sucks. – Rudie Apr 17 '11 at 14:14
  • 7
    From the new web site: "Update: the inline renderer is now a native part of the Text_Diff PEAR package. You don't need to use the hack presented here anymore." So just use Text_Diff now. – Mat Aug 17 '11 at 11:00
  • 13
    GPL isn't just free to use. It forces your module/project to be GPL as well. – Parris Apr 03 '13 at 17:49
  • 1
    Stumbled across this post whilst trying to implement a diffing mechanism for a project using mongodb document revisions. As @Mat mentioned the link in the answer is old, and will just direct you to here: http://www.horde.org/libraries/Horde_Text_Diff/download, but if you use the remi repo, you can do: `yum install php-horde-Horde-Text-Diff --enablerepo=remi` – Mike Purcell Dec 11 '13 at 00:28
  • what is the usage? – KD.S.T. May 29 '18 at 05:34
  • I'm wide lost, why this answer is useful, also the accepted answer? – Gustavo Rodríguez Jan 19 '21 at 17:22
27

This is a nice one, also http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/

Solving the problem is not as simple as it seems, and the problem bothered me for about a year before I figured it out. I managed to write my algorithm in PHP, in 18 lines of code. It is not the most efficient way to do a diff, but it is probably the easiest to understand.

It works by finding the longest sequence of words common to both strings, and recursively finding the longest sequences of the remainders of the string until the substrings have no words in common. At this point it adds the remaining new words as an insertion and the remaining old words as a deletion.

You can download the source here: PHP SimpleDiff...

gnat
  • 6,213
  • 108
  • 53
  • 73
Softy
  • 542
  • 7
  • 18
  • 1
    I found this very useful as well! Not as complicated as the Pear stuff. – dgavey Jan 03 '11 at 18:11
  • It gives me an errror here: `if($matrix[$oindex][$nindex] > $maxlen){` `Undefined variable: maxlen` – dynamic Apr 25 '11 at 11:27
  • Ok you posted a commetn to solve that. :) why you don't edit it in the initial code? Thanks anyway +1 ... hmm well you aren't the author – dynamic Apr 25 '11 at 12:25
  • 1
    here's what appears to be the latest version from 2010: https://github.com/paulgb/simplediff/blob/master/simplediff.php – rsk82 Jun 01 '11 at 18:45
  • 1
    Actually, +1 for simplicity – Parag Tyagi Nov 03 '14 at 13:32
  • 1
    This script can now be found here: [https://github.com/paulgb/simplediff/blob/master/php/simplediff.php](https://github.com/paulgb/simplediff/blob/master/php/simplediff.php) – Noel Whitemore Apr 06 '17 at 14:31
26

Here is a short function you can use to diff two arrays. It implements the LCS algorithm:

function computeDiff($from, $to)
{
    $diffValues = array();
    $diffMask = array();

    $dm = array();
    $n1 = count($from);
    $n2 = count($to);

    for ($j = -1; $j < $n2; $j++) $dm[-1][$j] = 0;
    for ($i = -1; $i < $n1; $i++) $dm[$i][-1] = 0;
    for ($i = 0; $i < $n1; $i++)
    {
        for ($j = 0; $j < $n2; $j++)
        {
            if ($from[$i] == $to[$j])
            {
                $ad = $dm[$i - 1][$j - 1];
                $dm[$i][$j] = $ad + 1;
            }
            else
            {
                $a1 = $dm[$i - 1][$j];
                $a2 = $dm[$i][$j - 1];
                $dm[$i][$j] = max($a1, $a2);
            }
        }
    }

    $i = $n1 - 1;
    $j = $n2 - 1;
    while (($i > -1) || ($j > -1))
    {
        if ($j > -1)
        {
            if ($dm[$i][$j - 1] == $dm[$i][$j])
            {
                $diffValues[] = $to[$j];
                $diffMask[] = 1;
                $j--;  
                continue;              
            }
        }
        if ($i > -1)
        {
            if ($dm[$i - 1][$j] == $dm[$i][$j])
            {
                $diffValues[] = $from[$i];
                $diffMask[] = -1;
                $i--;
                continue;              
            }
        }
        {
            $diffValues[] = $from[$i];
            $diffMask[] = 0;
            $i--;
            $j--;
        }
    }    

    $diffValues = array_reverse($diffValues);
    $diffMask = array_reverse($diffMask);

    return array('values' => $diffValues, 'mask' => $diffMask);
}

It generates two arrays:

  • values array: a list of elements as they appear in the diff.
  • mask array: contains numbers. 0: unchanged, -1: removed, 1: added.

If you populate an array with characters, it can be used to compute inline difference. Now just a single step to highlight the differences:

function diffline($line1, $line2)
{
    $diff = computeDiff(str_split($line1), str_split($line2));
    $diffval = $diff['values'];
    $diffmask = $diff['mask'];

    $n = count($diffval);
    $pmc = 0;
    $result = '';
    for ($i = 0; $i < $n; $i++)
    {
        $mc = $diffmask[$i];
        if ($mc != $pmc)
        {
            switch ($pmc)
            {
                case -1: $result .= '</del>'; break;
                case 1: $result .= '</ins>'; break;
            }
            switch ($mc)
            {
                case -1: $result .= '<del>'; break;
                case 1: $result .= '<ins>'; break;
            }
        }
        $result .= $diffval[$i];

        $pmc = $mc;
    }
    switch ($pmc)
    {
        case -1: $result .= '</del>'; break;
        case 1: $result .= '</ins>'; break;
    }

    return $result;
}

Eg.:

echo diffline('StackOverflow', 'ServerFault')

Will output:

S<del>tackO</del><ins>er</ins>ver<del>f</del><ins>Fau</ins>l<del>ow</del><ins>t</ins> 

StackOerverfFaulowt

Additional notes:

  • The diff matrix requires (m+1)*(n+1) elements. So you can run into out of memory errors if you try to diff long sequences. In this case diff larger chunks (eg. lines) first, then diff their contents in a second pass.
  • The algorithm can be improved if you trim the matching elements from the beginning and the end, then run the algorithm on the differing middle only. A latter (more bloated) version contains these modifications too.
Calmarius
  • 18,570
  • 18
  • 110
  • 157
25

If you want a robust library, Text_Diff (a PEAR package) looks to be pretty good. It has some pretty cool features.

Wickethewok
  • 6,534
  • 11
  • 42
  • 40
  • 6
    PHP Inline-Diff, mentioned above, "..uses Text_Diff from PEAR to compute a diff". :) – M.N Dec 11 '08 at 12:46
  • The link is broken. Cant find the package. This is the same Diff package used by the latest version of Wordpress. – Basil Musa Dec 01 '15 at 13:54
6

There is also a PECL extension for xdiff:

In particular:

Example from PHP Manual:

<?php
$old_article = file_get_contents('./old_article.txt');
$new_article = $_POST['article'];

$diff = xdiff_string_diff($old_article, $new_article, 1);
if (is_string($diff)) {
    echo "Differences between two articles:\n";
    echo $diff;
}
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    xdiff pecl extension is no longer maintained, apparently a stable release hasn't been done since 2008-07-01, according to http://pecl.php.net/package/xdiff, I ended up going with the suggestion by accepted answer as it's much newer, http://www.horde.org/libraries/Horde_Text_Diff/download – Mike Purcell Dec 11 '13 at 00:34
  • There are a simple install procedure for PHP's XDiff? (for Debian Linux) – Peter Krauss Oct 01 '14 at 13:50
  • @MikePurcell, as a matter of fact, it is still maintained. The latest stable version 2.0.1 supporting PHP 7 has been released on 2016-05-16. – user2513149 Jun 30 '17 at 11:33
  • @PeterKrauss, yes, there is. Take look at this question: https://serverfault.com/questions/362680/installing-xdiff-locally-with-apache-php – user2513149 Jun 30 '17 at 11:34
5

I had terrible trouble with the both the PEAR-based and the simpler alternatives shown. So here's a solution that leverages the Unix diff command (obviously, you have to be on a Unix system or have a working Windows diff command for it to work). Choose your favourite temporary directory, and change the exceptions to return codes if you prefer.

/**
 * @brief Find the difference between two strings, lines assumed to be separated by "\n|
 * @param $new string The new string
 * @param $old string The old string
 * @return string Human-readable output as produced by the Unix diff command,
 * or "No changes" if the strings are the same.
 * @throws Exception
 */
public static function diff($new, $old) {
  $tempdir = '/var/somewhere/tmp'; // Your favourite temporary directory
  $oldfile = tempnam($tempdir,'OLD');
  $newfile = tempnam($tempdir,'NEW');
  if (!@file_put_contents($oldfile,$old)) {
    throw new Exception('diff failed to write temporary file: ' . 
         print_r(error_get_last(),true));
  }
  if (!@file_put_contents($newfile,$new)) {
    throw new Exception('diff failed to write temporary file: ' . 
         print_r(error_get_last(),true));
  }
  $answer = array();
  $cmd = "diff $newfile $oldfile";
  exec($cmd, $answer, $retcode);
  unlink($newfile);
  unlink($oldfile);
  if ($retcode != 1) {
    throw new Exception('diff failed with return code ' . $retcode);
  }
  if (empty($answer)) {
    return 'No changes';
  } else {
    return implode("\n", $answer);
  }
}
user229044
  • 232,980
  • 40
  • 330
  • 338
xgretsch
  • 1,294
  • 13
  • 15
4

This is the best one I've found.

http://code.stephenmorley.org/php/diff-implementation/

enter image description here

Andrew
  • 3,733
  • 1
  • 35
  • 36
  • 3
    Doesn't work properly with UTF-8. It uses array access on strings, which treats each character as one byte wide. Should be easily fixable tough with mb_split. – Gellweiler May 25 '15 at 16:18
  • 1
    Here is a quick fix. Just replace `$sequence1 = $string1; $sequence2 = $string2; $end1 = strlen($string1) - 1; $end2 = strlen($string2) - 1;` with `$sequence1 = preg_split('//u', $string1, -1, PREG_SPLIT_NO_EMPTY); $sequence2 = preg_split('//u', $string2, -1, PREG_SPLIT_NO_EMPTY); $end1 = count($sequence1) - 1; $end2 = count($sequence2) - 1;` – Gellweiler May 25 '15 at 16:30
  • This class runs out of memory using character mode in function computeTable. – Andrew May 24 '17 at 19:42
  • 1
    The current link is http://code.iamkate.com/php/diff-implementation/. I've tested it and it doesn't support UTF-8. – Kangur May 16 '18 at 22:22
2

A php port of Neil Frasers diff_match_patch (Apache 2.0 licensed)

hakre
  • 193,403
  • 52
  • 435
  • 836
2

I would recommend looking at these awesome functions from PHP core:

similar_text — Calculate the similarity between two strings

http://www.php.net/manual/en/function.similar-text.php

levenshtein — Calculate Levenshtein distance between two strings

http://www.php.net/manual/en/function.levenshtein.php

soundex — Calculate the soundex key of a string

http://www.php.net/manual/en/function.soundex.php

metaphone — Calculate the metaphone key of a string

http://www.php.net/manual/en/function.metaphone.php

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
2

What you are looking for is a "diff algorithm". A quick google search led me to this solution. I did not test it, but maybe it will do what you need.

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
  • I've just tested that script and it works well - the diff operation completes very quickly (taking around 10ms to process the short paragraph I tested) and it was able to detect when a line break was added. Running the code as-is generates a couple of PHP notices which you might want to fix, but other than that it's a very good solution if you need to show the differences inline rather than use the traditional side-by-side diff view. – Noel Whitemore Apr 06 '17 at 13:53
1

I have tried a simple approach with two text box and some color styling. Note: my diff checker will only highlight difference in words and not in characters.

    <?php
    $valueOne = $_POST['value'] ?? "";
    $valueTwo = $_POST['valueb'] ?? "" ;
    
    $trimValueOne = trim($valueOne);
    $trimValueTwo = trim($valueTwo);

    $arrayValueOne = explode(" ",$trimValueOne);
    $arrayValueTwo = explode(" ",$trimValueTwo);

    $allDiff = array_merge(array_diff($arrayValueOne, $arrayValueTwo), array_diff($arrayValueTwo, $arrayValueOne));
    if(array_intersect($arrayValueOne,$allDiff) && array_intersect($arrayValueTwo,$allDiff)){

        if(array_intersect($arrayValueOne,$allDiff)){
            $highlightArr = array_intersect($arrayValueOne,$allDiff);
            $highlightArrValue = array_values($highlightArr);
            for ($i=0; $i <count($arrayValueOne) ;$i++) { 
                for ($j=0; $j <count($highlightArrValue) ; $j++) { 
                    if($arrayValueOne[$i] == $highlightArrValue[$j]){
                        $arrayValueOne[$i] = "<span>".$arrayValueOne[$i]."</span>";
                    }
                }
            }
            $strOne = implode(" ",$arrayValueOne);
            echo "<p class = \"one\">{$strOne}</p>";
        }if(array_intersect($arrayValueTwo,$allDiff)){
        $highlightArr = array_intersect($arrayValueTwo,$allDiff);
        $highlightArrValue = array_values($highlightArr);
        for ($i=0; $i <count($arrayValueTwo) ;$i++) { 
            for ($j=0; $j <count($highlightArrValue) ; $j++) { 
                    if($arrayValueTwo[$i] == $highlightArrValue[$j]){
                        $arrayValueTwo[$i] = "<span>".$arrayValueTwo[$i]."</span>";
                    }
                }
        }
        $strTwo = implode(" ",$arrayValueTwo);
        echo "<p class = \"two\">{$strTwo}</p>";
        }
    }elseif(!(array_intersect($arrayValueOne,$allDiff) && array_intersect($arrayValueTwo,$allDiff))){
        if($trimValueOne == $trimValueTwo){
            echo"<p class = \"one green\">$trimValueOne</p></p>";
            echo"<p class = \"two green\">$trimValueTwo</p></p>";
        }
        else{
            echo"<p class = \"one \">$trimValueOne</p></p>";
            echo"<p class = \"two \">$trimValueTwo</p></p>";
        }

    }
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./style.css">
</head>
<body>
    <form method="post" action="">
    <textarea type="text" name="value" placeholder="enter first text"></textarea>
    <textarea type="text" name="valueb" placeholder="enter second text"></textarea>
    <input type="submit">
    </form>
</body>
</html>
0

I came across this PHP diff class by Chris Boulton based on Python difflib which could be a good solution:

PHP Diff Lib

0

Another solution (for side-by-side comparison as opposed to a unified view): https://github.com/danmysak/side-by-side.

Danylo Mysak
  • 1,514
  • 2
  • 16
  • 22
0

For those just looking for a very simple function to find characters in string A but not in string B i wrote this quick and very simple function.

function strdiff($a,$b){

    $a = str_split($a);
    $b = str_split($b);

    return array_diff($a,$b);

}
Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36
0

Hi this will help you a lot:

$old_data = "We'll of today's hunt we will find inner zen. You are awesome [TEAM_NAME]! Cleveland has a lot more to offer though, so keep on roaming and find some happiness with Let's Roam!;";
$new_data = "We'll of today's hunt we will find inner zen. Great job today, you are freaking super awesome [TEAM_NAME]! though, so keep roaming Cleveland has a lot more to offer and find happiness on www.letsroam.com!;";

if($old_data) {
  $old_words = explode(" " , $old_data);
  $new_words = explode(" ", $new_data);

  $added_words = array();
  $deleted_words = array();
  $unchanged_words = array();
  foreach($new_words as $new_word) {
      $new_word_index = array_search($new_word, $old_words);
      // if($new_word == "you"){
      //   die_r(array());
      // }
      if( $new_word_index > -1) {
          // word already exists
          array_push($unchanged_words, $new_word);
          unset($old_words[$new_word_index]);
      } else {
          // word does not already exists
          array_push($added_words, $new_word);
      } 
      
   }
 $deleted_words = $old_words;
 $added_word_count = count($added_words);
 $added_word_characters = strlen(implode(" ", $added_words));
}
die_r(array(
  "old_data"=> $old_data,
  "new_data"=> $new_data,
  "unchanged_words"=> $unchanged_words,
  "added_words"=> $added_words,
  "deleted_words"=> $deleted_words,
  "added_word_count"=>$added_word_count,
  "added_word_characters"=>$added_word_characters
));
0

Try https://methodfish.com/Projects/MFStrDiff Download class.MFStrDiff.php into your path and then use the following code to generate a word level comparison:

include_once("class.MFStrDiff.php");
$diff=new MFStrDiff();
$diffHtml = $diff->getDiff($tx1, $tx2); // HTML output by default

See demo on https://methodfish.com/Projects/MFStrDiff/demo

user1432181
  • 918
  • 1
  • 9
  • 24