Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated the website and our policies to make sure your privacy rights and security are respected.
Click here to learn more about the way our website handles your data.

Remove this message.

Using xdiff to create your own diff tool in PHP

Difficulty: 25 / 50 Tweet
spot-the-difference

The xdiff extension is a great way to monitor and patch files or strings containing differences between different revisions. This has proven to be very useful for me on a project where I needed to implement a monitoring system for an editor. The man wanted to be able to track the historic contribution that each of his authors and proofreaders had on an article. Basically it was a git tool for journalists.

While xdiff can also do binary pacthing, I won't be focusing on that in this tutorial. Instead I will show you how to install and use the extension to compare strings.

A few things about how to install xdiff

If you try to install the PECL extension directly you will most likely get an error about the fact that the libxdiff library is missing. At least this is what happened to me when I tried it on an Ubuntu 14.04 server. This requirement is also documented here, but who reads these things... right ?

  
    cd /usr/src
    wget http://www.xmailserver.org/libxdiff-0.22.tar.gz
    tar -xzf libxdiff-0.22.tar.gz
    cd libxdiff-0.22
    ./configure
    make
    make install
  

To install libxdiff type the commands listed above, but first check to see if there's a newer version of libxdiff available.

Then type pecl install xdiff-1.5.2 instead of pecl install xdiff. In the latter case the compilation will fail as documented here.

Now open up your php.ini file and paste this in the extensions section: extension=xdiff.so. Then reload apache (service apache2 reload) and test your new configuration with the code below - it will return false on failure.

  
    <?php
      var_dump(function_exists('xdiff_string_diff'));
    ?>
  

Basic usage

Here's an example that shows how xdiff can display the differences between two strings and then "patch" the old string. The strings represent the first paragraph of the Wikipedia article about PHP as it is now (January 2015) and how it was in December 2013.

  
    <?php 
    $new = <<<HTML
          PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.
          As of January 2013, PHP was installed on more than 240 million websites (39% of those sampled) and 2.1 million web servers.
          Originally created by Rasmus Lerdorf in 1994, the reference implementation of PHP (powered by the Zend Engine) is now produced by The PHP Group.
HTML;

    $old = <<<HTML
          PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.
          PHP is now installed on more than 244 million websites and 2.1 million web servers.
          Originally created by Rasmus Lerdorf in 1995, the reference implementation of PHP is now produced by The PHP Group.
HTML;

    echo '<pre>';
    $diff = xdiff_string_diff($old, $new);
    if ($diff!==FALSE)
      echo $diff;
    /*
      @@ -1,3 +1,3 @@
           PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.
    -      PHP is now installed on more than 244 million websites and 2.1 million web servers.
    -      Originally created by Rasmus Lerdorf in 1995, the reference implementation of PHP is now produced by The PHP Group.
    \ No newline at end of file
    +      As of January 2013, PHP was installed on more than 240 million websites (39% of those sampled) and 2.1 million web servers.
    +      Originally created by Rasmus Lerdorf in 1994, the reference implementation of PHP (powered by the Zend Engine) is now produced by The PHP Group.
    \ No newline at end of file
    */
    $errors = '';

    $patched = xdiff_string_patch($old, $diff, XDIFF_PATCH_NORMAL, $errors);
    if ($patched!==FALSE)
      echo $patched;
    ?>
  
comments powered by Disqus