Download the PHP package baraja-core/simple-php-diff without Composer
On this page you can find all versions of the php package baraja-core/simple-php-diff. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download baraja-core/simple-php-diff
More information about baraja-core/simple-php-diff
Files in baraja-core/simple-php-diff
Package simple-php-diff
Short Description Find the quick difference between two text files in PHP.
License
Homepage https://github.com/baraja-core/simple-php-diff
Informations about the package simple-php-diff
Simple PHP Diff
A lightweight PHP library for fast text comparison and difference visualization. Find the quick difference between two text files and render the results in plain text or HTML format.
Key Principles
- Line-by-line comparison - Compares texts line by line with numbered output for easy change tracking
- Immutable result object - Returns a
Diffobject containing original, target, formatted diff, and changed line numbers - Dual output modes - Plain text diff output and styled HTML rendering with color-coded changes
- Strict mode support - Optional strict comparison that preserves different line ending formats
- Whitespace visualization - Pretty rendering shows tabs as arrows and spaces as dots for clarity
- Zero dependencies - Pure PHP implementation requiring only PHP 8.0+
Architecture
The library consists of two main components with a clean separation of concerns:
Components
SimpleDiff
The main comparison engine that processes two text inputs and generates a diff result.
Methods:
| Method | Description |
|---|---|
compare(string $left, string $right, bool $strict = false): Diff |
Compares two strings and returns a Diff object |
renderDiff(Diff\|string $diff): string |
Renders the diff as styled HTML |
Comparison Process:
- Input normalization (non-strict mode): Converts all line endings (
\r\n,\r) to\nand trims whitespace - Line splitting: Splits both inputs into arrays by newline character
- Line-by-line comparison: Iterates through lines, comparing original vs target
- Output formatting: Prepends each line with status marker (
+,-, or space) and line number - Change tracking: Records line numbers where differences occur
Diff
An immutable value object that encapsulates the comparison result.
Properties (via getters):
| Property | Type | Description |
|---|---|---|
original |
string |
The normalized left/original input text |
target |
string |
The normalized right/target input text |
diff |
string |
The formatted diff output with line markers |
changedLines |
int[] |
Array of line numbers (1-indexed) that differ |
String Conversion:
The Diff object implements __toString() which returns the formatted diff string, allowing direct string casting.
📦 Installation
It's best to use Composer for installation, and you can also find the package on Packagist and GitHub.
To install, simply use the command:
You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework.
Requirements
- PHP 8.0 or higher
Basic Usage
Simple Text Comparison
Get Changed Line Numbers
Render Diff as HTML
Output Format
Plain Text Output
The diff output uses a standardized format:
Format explanation:
- Lines starting with
(two spaces) are unchanged - Lines starting with
-indicate content from the original (left) text - Lines starting with
+indicate content from the target (right) text - Line numbers are right-padded and followed by
| - Spaces are rendered as
·(middle dot) - Tabs are rendered as
→→→→(four arrows)
HTML Output
The renderDiff() method generates HTML with inline styles:
Color coding:
- Green background (
#a2f19c): Added lines (prefixed with+) - Red background (
#e7acac): Removed lines (prefixed with-) - No background: Unchanged lines
Visual Examples
Plain Text Diff Output
HTML Rendered Diff
Comparison Modes
Non-Strict Mode (Default)
In non-strict mode (default), the library normalizes line endings before comparison:
- Converts
\r\n(Windows) to\n - Converts
\r(old Mac) to\n - Trims leading and trailing whitespace from both inputs
This mode is ideal for comparing content where line ending differences should be ignored.
Strict Mode
Strict mode preserves the original line endings and whitespace, useful when you need to detect differences in line termination characters.
Working with the Diff Object
Accessing Original and Target Text
Getting the Raw Diff String
Working with Changed Lines
Advanced Examples
Comparing Files
Custom HTML Rendering
If you need custom styling, you can process the diff string yourself:
Integration with Version Control Display
Author
Jan Barášek - https://baraja.cz
📄 License
baraja-core/simple-php-diff is licensed under the MIT license. See the LICENSE file for more details.