Overview
A zero-dependency, LCS-based line-by-line text diffing library for HolyPHP.
bundled — works offline
zero dependencies
patch
lcs
Quick start
import "diff";
$old = "line 1\nline 2\nline 3\n";
$new = "line 1\nline 2 modified\nline 3\nline 4\n";
$unified = diff_unified($old, $new, "old.txt", "new.txt");
println($unified);
$stats = diff_stats($old, $new);
println("Added: " . $stats["added"] . ", Removed: " . $stats["removed"]);
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install diff
This adds the library to hphp.yaml, installs it into .hphp/packages/diff/ and pins the exact version in hphp.lock:
require:
diff: "^1.0"
Use it
import "diff";
diff is bundled with the compiler, so import "diff"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install diff # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install diff
Documentation
API Reference
diff_lines(string $old, string $new): array
Computes a line-by-line diff using the Longest Common Subsequence (LCS) algorithm.
- Parameters:
$old (string): Original text.
$new (string): Modified text.
- Returns:
array of diff operation maps, each containing:
"type" (string): "equal", "add", or "remove".
"line" (string): The line content (without trailing newline).
"old_line" (int|null): 1-based line number in $old (null for "add").
"new_line" (int|null): 1-based line number in $new (null for "remove").
diff_unified(string $old, string $new, string $oldLabel = "a", string $newLabel = "b", int $context = 3): string
Generates a standard unified diff string with hunk headers (@@ -oldStart,oldCount +newStart,newCount @@).
- Parameters:
$old (string): Original text.
$new (string): Modified text.
$oldLabel (string, optional): Header label for old text (default "a").
$newLabel (string, optional): Header label for new text (default "b").
$context (int, optional): Number of unchanged context lines around each change (default 3).
- Returns:
string — Unified diff output, or "" if there are no differences.
diff_stats(string $old, string $new): array
Returns summary counts of line changes between $old and $new.
- Parameters:
$old (string): Original text.
$new (string): Modified text.
- Returns:
array with keys:
"added" (int): Number of added lines.
"removed" (int): Number of removed lines.
"unchanged" (int): Number of unchanged lines.
"total" (int): Total number of diff operations.
diff_has_changes(string $old, string $new): bool
Returns true if $old and $new differ on any line, false otherwise.
Error Handling
All functions validate their inputs and throw an Exception with a clear diff: ... message on invalid arguments:
diff: context must be a non-negative integer — thrown by diff_unified() when $context < 0.
diff: old text must be a string / diff: new text must be a string — thrown when non-string inputs are passed.
Examples
01Quick start
Bundled with the compiler — import "diff"; works offline in any file, or add it to a project with hphp install diff.
import "diff";
$old = "line 1\nline 2\nline 3\n";
$new = "line 1\nline 2 modified\nline 3\nline 4\n";
$unified = diff_unified($old, $new, "old.txt", "new.txt");
println($unified);
$stats = diff_stats($old, $new);
println("Added: " . $stats["added"] . ", Removed: " . $stats["removed"]);
Try ithphp install diff
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_diff.hphp.