<?php
// old and new strings
$old = "Foo \r\nFoo\r\nBar\r\nQux";
$new = "Foo\nBar\nBaz";
// convert all line endings to the same type
$old = str_replace(["\r\n", "\r"], "\n", $old);
$new = str_replace(["\r\n", "\r"], "\n", $new);
// seperate into lines
$old_lines = explode("\n", $old);
$new_lines = explode("\n", $new);
// optional: remove whitespace so spaces don't trigger falsely
array_walk($old_lines, function(&$value) {
$value = rtrim($value);
});
array_walk($new_lines, function(&$value) {
$value = rtrim($value);
});
// figure out which one is longer
if (count($old_lines) >= count($new_lines)) {
$primary = $old_lines;
$secondary = $new_lines;
} else {
$primary = $new_lines;
$secondary = $old_lines;
}
// compare line by line
$results = [];
foreach ($primary as $key => $value) {
if (key_exists($key, $secondary)) {
if ($primary[$key] == $secondary[$key]) {
$results[] = "MATCH";
} else {
$results[] = "DIFFERENT";
}
} else {
$results[] = "MISSING";
}
}
var_dump($old_lines);
var_dump($new_lines);
var_dump($results);
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- array(4) {
[0]=>
string(3) "Foo"
[1]=>
string(3) "Foo"
[2]=>
string(3) "Bar"
[3]=>
string(3) "Qux"
}
array(3) {
[0]=>
string(3) "Foo"
[1]=>
string(3) "Bar"
[2]=>
string(3) "Baz"
}
array(4) {
[0]=>
string(5) "MATCH"
[1]=>
string(9) "DIFFERENT"
[2]=>
string(9) "DIFFERENT"
[3]=>
string(7) "MISSING"
}
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array(4) {
[0]=>
string(3) "Foo"
[1]=>
string(3) "Foo"
[2]=>
string(3) "Bar"
[3]=>
string(3) "Qux"
}
array(3) {
[0]=>
string(3) "Foo"
[1]=>
string(3) "Bar"
[2]=>
string(3) "Baz"
}
array(4) {
[0]=>
string(5) "MATCH"
[1]=>
string(9) "DIFFERENT"
[2]=>
string(9) "DIFFERENT"
[3]=>
string(7) "MISSING"
}
preferences:
155.71 ms | 402 KiB | 298 Q