3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on 5W5ib
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.3.1
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" }