Once I needed a decent text diffing solution for SafePatch and after some searching couldn’t find one. So I’ve decided to write my own. It might not be very accurate but it’s very easy to use and has two modes – finding changes by line or inline (within one line). It also comes with built-in HTML generation although you need to style it yourself with CSS.

It has no dependencies and works out of the box in PHP 5.2 and up – simply include it and you’re ready to go.

Download QuickDiff here. Released in public domain. You can also check/fork/report issues to GitHub.

Class methods

InlineDiffes two flat strings treating line breaks as regular characters. Returns an array; each member is an array which 1st element specifies the type of difference found (- for removed piece, + for added, = for equal and change when a string portion was altered) and 2nd element holds what was deleted/added/equal. For change also a 3rd element is present indicating the new value of that string portion.
InlineHTMLCalls Inline() to calculate the difference and then transforms its result into a HTML string with xml<span> tags around each changed portion with class indicating the type of change (del, add, equ or change).
FullDiffes two texts by-line. Returns an array; each member has similar form to that of Inline().
FullHTMLCalculates difference between two texts by calling Full() and returns a HTML string. Collapses lengthy runs of equal lines and calculates in-line differences with Inline(). Its $options array can have these members:
  • classes – maps piece types (like - and +) onto tag class names (like del and add; note that it only affects by-line differ, not InlineHTML() which uess hardcoded mappings
  • lineNumbers – a boolean indicating if line numbers should be output or not
  • collapseLines – a number specifying how long a sequence of unchanged lines can be; longer sequences get a dashed-out separator in place of omitted lines
FullHtmlWrappedSimilar to FullHTML() but wraps the entire listing into a xml<pre> tag, also checking whether or not an input is binary and outputting a message without diffing it if it’s the case. Its $options array can have these members plus all those of FullHTML():
  • tag – tag name to wrap the listing into
  • tagClasses – classes to set to it, e.g. xml<pre class="qdiff">; can be empty
  • binMsg – message string to output if the input is binary; it’s output as a xml<p> tag having tagClasses plus added «bin» class

HTML-outputting functions convert certain symbols defined in PHPQuickDiff::$htmlReplaces to more representable text equivalents. Particularly, spaces become middle dots (·), line breaks – xml<br /> and tabs become two spaces (no ·).

Usage examples

require_once 'quickdiff.php';

//=> <pre class="qdiff">...</pre>
echo QuickDiff::FullHtmlWrapped("\bin\xA\ry\0u\tput""line1\nKABOOM\nline2");
//=> <p class="qdiff bin">Diff is unavailable for binary data.</p>
echo QuickDiff::InlineHTML("this plus that""plus that and this");
//=> <span class="add">this<i>&middot;</i></span>...