How to compare text and mark differences using PHP

I was looking for a simple class which will compare to strings and return a result with marked differences. What chars or words where removed and which were added. Just like wiki compare revisions. Actually i googled a lot and finally found a result:
The main class which do mostly all the job is Text_Diff, you can download it from http://pear.php.net/package/Text_Diff/

The other tool I used was http://software.zuavra.net/inline-diff/. It use Text_Diff and render results.
If you have two strings: “my blue pen” and “my red pen”, this is the diff and it will produce: “my <del>blue</del><ins>red</ins> pen”.
It does the following:

  • takes two strings
  • splits each of them into arrays containing one character per entry
  • considers the two arrays to be “lines” and uses Text_Diff from PEAR to compute a diff
  • it uses my own diff renderer which extends the one from Text_Diff and renders the differences “inline” using <del></del> and <ins></ins> to mark them. These tags are customizable.
  • finally, it returns the string consisting of the two originals mashed together, with the inline diffs applied.

Result example:
If you have two strings: “my blue pen” and “my red pen”, this is the diff and it will produce: “my bluered pen”.

10 Comments

  1. Yoda says:

    thanks

  2. FridrihLop says:

    Good article, the advertisment is sold?

  3. Tuongvi87 says:

    Hi i can’t take output formatted string into one variable. I have been using text_diff of pear. I don’t know how to convert formatted string of text_diff output to a string because i ưant to foist it in to $string.
    please, help me.

  4. Algirdas says:


    include_once 'inline_function.php';
    $nl = '#**!)@#';
    $text_with_marked_differences = inline_diff($original_text, $modified_text, $nl);

  5. lenselijer says:

    When I use:

    include_once ‘inline_function.php’;
    $nl = ‘#**!)@#’;
    $a = ’1 1′;
    $b = ’1 2′;
    $differences = inline_diff($a, $b, $nl);

    The output of $differences is already shown on the screen, and i dont put echo before it. Can it save the output to the variable instead?.

    I want to use $differences later on in my php code by using:
    $message = str_replace(‘{content}’, $differences, $message);
    echo $message;

    Is this possible? I think it has something to do with echo “$line “; in inline_renderer.php.

    I hope you can help me.

  6. Algirdas says:

    For me there’s nothing shown on screen. I copy/pasted my code where I use it. I don’t know where is your problem.

  7. pepito says:

    lenselijer,

    I’ve got the same problem as you (it echoes before going to variable and the variable is then empty).
    Did you find a solution ? If so, what was it ?

    Thx !

  8. Echo says:

    There’s an easy solution to this problem:

    Hope this helps ;)
    ?>

  9. Echo says:

    Hmm… it appears this comment box strips tags, including my PHP example. Here it is again, without PHP tags.

    include_once 'inline_function.php';
    $nl = '#**!)@#';
    $a = '1 1';
    $b = '1 2';
    ob_start();
    inline_diff($a, $b, $nl);
    $differences = ob_get_contents();
    ob_end_clean();
    
    // $differences now contains the result, and nothing has been output to the browser.
    

  10. Xandr says:

    Thanks, used :)

    But Text_Diff has many warnings in PHP5

Leave a Reply