PHP CSV Files
CSV has quoting rules that a simple comma split cannot represent. Use the CSV APIs with explicit separator, enclosure and escape choices.
How it works
A field can contain a comma or a quotation mark. fputcsv and fgetcsv implement the format rather than requiring each caller to invent escaping. This lab explicitly passes an empty escape string, avoiding reliance on the deprecated default escape parameter in newer PHP versions. CSV imported into spreadsheet software also needs a separate formula-injection policy; correct CSV quoting is not that policy.
Run it locally
Run this complete source locally with PHP 8.2 or newer. Save it using the filename shown and run the command below. PHP is not currently available in Bhylu’s browser playground. Apache and MySQL are not needed for an ordinary CLI example.
php -l csv.php
php csv.phpDownload this source as a local PHP lab file. Keep lab files separate from the Bhylu project and production data.
Example
<?php
declare(strict_types=1);
$stream = fopen("php://temp", "w+");
if ($stream === false) throw new RuntimeException("No stream");
try {
fputcsv($stream, ["Hand drill, small", "2"], ",", '"', "");
rewind($stream);
$row = fgetcsv($stream, null, ",", '"', "");
if ($row === false) throw new RuntimeException("No row");
echo $row[0], " | ", $row[1], "\n";
} finally { fclose($stream); }
Expected result
Hand drill, small | 2The comma inside the first field survives a write/read round trip as part of one value. The quantity remains text after CSV parsing and still needs numeric validation. php://temp keeps this small temporary stream under the runtime’s ownership; it is not a promise of permanently saving the import.
Check the official PHP manual for detailed version and API contracts.