<?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);
preferences:
53.85 ms | 402 KiB | 5 Q