-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-text-comparison.php
More file actions
72 lines (54 loc) · 1.23 KB
/
basic-text-comparison.php
File metadata and controls
72 lines (54 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
/*
* This file is part of the ALTO library.
*
* © 2025–present Simon André
*
* For full copyright and license information, please see
* the LICENSE file distributed with this source code.
*/
require __DIR__.'/../../vendor/autoload.php';
use Alto\Code\Diff\Diff;
use Alto\Code\Diff\Renderer\UnifiedRenderer;
echo "=== 1. Simple Line Diff ===\n";
$old = <<< 'EOF'
Hello
World
EOF;
$new = <<< 'EOF'
Hello
PHP
World
EOF;
$result = Diff::build()->compare($old, $new);
$renderer = new UnifiedRenderer();
echo $renderer->render($result)."\n";
echo "\n=== 2. Word-Level Changes ===\n";
$old = 'The quick brown fox';
$new = 'The fast brown fox';
$result = Diff::build()
->withWordDiff()
->compare($old, $new);
foreach ($result->hunks() as $hunk) {
foreach ($hunk->edits as $edit) {
if ($edit->wordSpans) {
foreach ($edit->wordSpans as $span) {
echo "{$span->op}: {$span->text}\n";
}
}
}
}
echo "\n=== 3. Ignore Whitespace ===\n";
$old = <<< 'EOF'
line1
line2
EOF;
$new = <<< 'EOF'
line1
line2
EOF;
$result = Diff::build()
->ignoreWhitespace()
->compare($old, $new);
echo 'Is empty? '.($result->isEmpty() ? 'Yes' : 'No')."\n";