PHP code example of sitefinitysteve / php-diff-text
1. Go to this page and download the library: Download sitefinitysteve/php-diff-text library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sitefinitysteve / php-diff-text example snippets
use PhpDiffText\DiffText;
// One-liner — pick your strategy
echo DiffText::words('The quick brown fox', 'The slow brown fox');
echo DiffText::chars('cat', 'car');
echo DiffText::lines($oldFile, $newFile);
use PhpDiffText\DiffWords;
echo DiffWords::render('The quick brown fox', 'The slow brown fox');
// Get raw Change[] array for custom rendering
$changes = DiffWords::diff('old text', 'new text');
use PhpDiffText\DiffText;
public function show()
{
return view('document.diff', [
'diffHtml' => DiffText::words($oldVersion, $newVersion),
]);
}
use PhpDiffText\DiffText;
// Word-level diff (default)
echo DiffText::html('<p>Hello world</p>', '<p>Hello Vue world</p>');
// Full replacement when texts are very different
echo DiffText::html(
'Original long paragraph about insurance policies...',
'Item 1: House. Item 2: Car.',
similarityThreshold: 0.3
);
blade
{{-- Include the diff styles (in your layout or the specific view) --}}
<link rel="stylesheet" href="{{ asset('vendor/php-diff-text/style.css') }}">
{{-- Render the diff output (already safe HTML) --}}
{!! $diffHtml !!}