'
50 | . htmlspecialchars((string) $diff)
51 | . '
';
52 | ```
53 |
54 | The `compare()` method returns a complete object `Diff` with the results of the comparison, from which you can get much more.
55 |
56 | For example, to get a list of changed rows:
57 |
58 | ```php
59 | echo 'Changed lines: ';
60 | echo implode(', ', $diff->getChangedLines());
61 | ```
62 |
63 | Display the Diff in HTML
64 | ------------------------
65 |
66 | Very often we need to display the differences directly in the browser, for this the native method `renderDiff()` is suitable.
67 |
68 | ```php
69 | $left = 'First text';
70 | $right = 'Second text';
71 |
72 | $simpleDiff = new \Baraja\DiffGenerator\SimpleDiff;
73 | $diff = $simpleDiff->compare($left, $right);
74 |
75 | echo $simpleDiff->renderDiff($diff);
76 | ```
77 |
78 | The method accepts Diff and returns valid treated HTML that can be displayed directly to the user.
79 |
80 | Comparison mode
81 | ---------------
82 |
83 | This tool supports strict and basic comparison modes (strict mode is disabled by default).
84 | Strict mode also allows you to compare changes in different line wrapping methods (for example, `"\n"` and so on).
85 |
86 |
87 | 📄 License
88 | -----------
89 |
90 | `baraja-core/simple-php-diff` is licensed under the MIT license. See the [LICENSE](https://github.com/baraja-core/template/blob/master/LICENSE) file for more details.
91 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "baraja-core/simple-php-diff",
3 | "description": "Find the quick difference between two text files in PHP.",
4 | "homepage": "https://github.com/baraja-core/simple-php-diff",
5 | "authors": [
6 | {
7 | "name": "Jan Barášek",
8 | "homepage": "https://php.baraja.cz"
9 | }
10 | ],
11 | "require": {
12 | "php": "^8.0"
13 | },
14 | "require-dev": {
15 | "phpstan/phpstan": "^1.0",
16 | "phpstan/extension-installer": "^1.1",
17 | "phpstan/phpstan-nette": "^1.0",
18 | "phpstan/phpstan-deprecation-rules": "^1.0",
19 | "phpstan/phpstan-strict-rules": "^1.0",
20 | "spaze/phpstan-disallowed-calls": "^2.0",
21 | "roave/security-advisories": "dev-master"
22 | },
23 | "autoload": {
24 | "classmap": [
25 | "src/"
26 | ]
27 | },
28 | "scripts": {
29 | "phpstan": [
30 | "vendor/bin/phpstan analyse src -c phpstan.neon --level 9 --no-progress"
31 | ]
32 | },
33 | "minimum-stability": "stable",
34 | "config": {
35 | "allow-plugins": {
36 | "phpstan/extension-installer": true
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/doc/diff-to-html.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baraja-core/simple-php-diff/612e9a1429fd820df7067cb2753b7d7561dab12e/doc/diff-to-html.png
--------------------------------------------------------------------------------
/doc/simple-diff.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baraja-core/simple-php-diff/612e9a1429fd820df7067cb2753b7d7561dab12e/doc/simple-diff.png
--------------------------------------------------------------------------------
/phpstan.neon:
--------------------------------------------------------------------------------
1 | includes:
2 | - vendor/spaze/phpstan-disallowed-calls/disallowed-dangerous-calls.neon
3 | - vendor/spaze/phpstan-disallowed-calls/disallowed-execution-calls.neon
4 |
5 | parameters:
6 | checkMissingIterableValueType: false
7 |
--------------------------------------------------------------------------------
/src/Diff.php:
--------------------------------------------------------------------------------
1 | $changedLines
12 | */
13 | public function __construct(
14 | private string $original,
15 | private string $target,
16 | private string $diff,
17 | private array $changedLines,
18 | ) {
19 | }
20 |
21 |
22 | public function __toString(): string
23 | {
24 | return $this->diff;
25 | }
26 |
27 |
28 | public function getOriginal(): string
29 | {
30 | return $this->original;
31 | }
32 |
33 |
34 | public function getTarget(): string
35 | {
36 | return $this->target;
37 | }
38 |
39 |
40 | public function getDiff(): string
41 | {
42 | return $this->diff;
43 | }
44 |
45 |
46 | /**
47 | * @return array