I've used the @rickdenhaan solution and I've found a problem with arsort() and PHP Version.
if eol is "\r\n" the $eols array will be:
array("\r\n" => 1, "\r" => 1, "\n" => 1);
(because in addition to 1 "\r\n" is also found 1 "\r" and 1 "\n")
and with PHP 7, after arsort($eols), the keys order is the same:
array("\r\n" => 1, "\r" => 1, "\n" => 1);
and after the "$eol = key($eols);" $eol will be "\r\n"
But with PHP 5.6, after arsort($eols), the keys order is:
array("\n" => 1, "\r" => 1, "\r\n" => 1);
and after the "$eol = key($eols);" $eol will be "\n"
I've solved with this check after "$eol = key($eols);":
if (($eols["\r\n"] == $eols["\r"]) AND ($eols["\r\n"] == $eols["\n"])) {
$line_separator = "\r\n";
}