PHP 8.5.0 Alpha 1 available for testing

Voting

: min(three, zero)?
(Example: nine)

The Note You're Voting On

hans at loltek dot net
1 year ago
old MacOS (up to ~2001) and old Office For MacOS (up to 2007? I think) use carriage-return for newlines,
Microsoft Windows use carriage-return+line-feed for newlines,
Unix (Linux and modern MacOS) use line-feeds,
Some systems use BOM/byte-order-masks just to say they use UTF-8, i've even encountered one-BOM-per-CSV-row!

For a csv-file parser handling all the above cases, I wrote:

<?php
function parse_csv(string $csv, string $separator = ","): array
{
$csv = strtr(
$csv,
[
"\xEF\xBB\xBF" => "", // remove UTF-8 byte order masks, if present
"\r\n" => "\n", // Windows CrLf=> Unix Lf
"\r" => "\n" // old-MacOS Cr => Unix Lf
// (both modern MacOS and Linux use Lf .. Windows is the only outlier)
]
);
$lines = explode("\n", $csv);
$keys = str_getcsv(array_shift($lines), $separator);
$ret = array();
foreach (
$lines as $lineno => $line) {
if (
strlen($line) < 1) {
// ... probably malformed csv, but we'll allow it
continue;
}
$parsed = str_getcsv($line, $separator);
if (
count($parsed) !== count($keys)) {
throw new
\RuntimeException("error on csv line #{$lineno}: count mismatch:" . count($parsed) . ' !== ' . count($keys) . ": " . var_export([
'error' => 'count mismatch',
'keys' => $keys,
'parsed' => $parsed,
'line' => $line
], true));
}
$ret[] = array_combine($keys, $parsed);
}
return
$ret;
}
?>

<< Back to user notes page

To Top