Overview
RFC 4180 CSV parser and writer for HolyPHP with header support, custom delimiters, quoting/escaping, and associative row conversion.
bundled — works offline
spreadsheet
tsv
rows
Quick start
import "csv";
$input = "id,name,bio\r\n1,Alice,\"Likes \"\"CSV\"\", tea\"\r\n2,Bob,Developer\r\n";
// Parse into associative rows using the first row as headers
$users = csv_parse_assoc($input);
println($users[0]["name"]); // Alice
println($users[0]["bio"]); // Likes "CSV", tea
// Serialize back to CSV
$out = csv_stringify_assoc($users, ["id", "name", "bio"]);
println($out);
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install csv
This adds the library to hphp.yaml, installs it into .hphp/packages/csv/ and pins the exact version in hphp.lock:
require:
csv: "^1.0"
Use it
import "csv";
csv is bundled with the compiler, so import "csv"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install csv # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install csv
| Package | csv |
| Version | 1.0.0 (follows the compiler) |
| Entry file | csv.hphp |
| Source | packages/lib/csv ↗ |
Documentation
API Reference
csv_parse(string $input, string $delimiter = ",", string $quote = "\""): array
Parses a CSV string into a 2D array of string fields.
- Supports CRLF (
\r\n), LF (\n), and CR (\r) line endings.
- Handles quoted fields containing delimiters, newlines, and escaped quotes (
"").
- Returns
[] for an empty string.
csv_parse_assoc(string $input, string $delimiter = ",", string $quote = "\""): array
Parses a CSV string using the first row as header keys and returns an array of associative arrays.
- Returns
[] for an empty string.
- Throws an
Exception if headers contain duplicates or if any data row has a different column count than the header row.
csv_stringify(array $rows, string $delimiter = ",", string $quote = "\"", string $newline = "\r\n", bool $quoteAll = false): string
Serializes a 2D array of scalar values (string, int, float, bool, null) into a CSV string.
- Automatically quotes fields containing
$delimiter, $quote, \r, or \n (or all fields when $quoteAll is true).
- Escapes embedded
$quote characters by doubling them.
csv_stringify_assoc(array $rows, array $headers = [], string $delimiter = ",", string $quote = "\"", string $newline = "\r\n", bool $quoteAll = false): string
Serializes an array of associative arrays into a CSV string with a header row.
- If
$headers is empty, headers are inferred from the keys of the first row.
- Missing keys in any row are serialized as empty strings
"".
Error Handling
All functions validate their inputs and throw Exception with clear, csv:-prefixed messages:
csv: delimiter must be a single character
csv: quote must be a single character
csv: delimiter and quote characters must be different
csv: newline must be "\r\n", "\n", or "\r"
csv: unclosed quoted field starting at line <N>
csv: unexpected quote in unquoted field at line <N>, column <M>
csv: unexpected character after closing quote at line <N>, column <M>
csv: duplicate header "<name>"
csv: row <N> has <M> columns, expected <K> to match headers
csv: cannot serialize non-scalar cell value
Examples
01Quick start
Bundled with the compiler — import "csv"; works offline in any file, or add it to a project with hphp install csv.
import "csv";
$input = "id,name,bio\r\n1,Alice,\"Likes \"\"CSV\"\", tea\"\r\n2,Bob,Developer\r\n";
// Parse into associative rows using the first row as headers
$users = csv_parse_assoc($input);
println($users[0]["name"]); // Alice
println($users[0]["bio"]); // Likes "CSV", tea
// Serialize back to CSV
$out = csv_stringify_assoc($users, ["id", "name", "bio"]);
println($out);
Try ithphp install csv
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_csv.hphp.