<?php
/*
* You can open a sample spreadsheet at https://goo.gl/j50MKc
* You can download a sample CSV at https://goo.gl/H4ni63
*/
$payload = '"Hello\", World!';
$rfcCsv = '"""Hello\"", World!"'; //Contents of https://goo.gl/H4ni63
$actualRfc = str_getcsv($rfcCsv, ',', '"', '"');
echo "READING RFC CSV\n";
echo "---------------\n";
echo "To read a well formatted CSV, you must use the `double-quote` char as enclosure and\n";
echo " as escape\n";
echo sprintf("Expected: %s\n", $payload);
echo sprintf("Actual : %s\n", $actualRfc[0]);
echo "Equals? "; var_dump($payload === $actualRfc[0]);
echo "\n\n================================================================================\n\n";
$handler = fopen('php://temp', 'w+');
echo "WRITING\n";
echo "-------\n";
if (PHP_VERSION_ID < 50504 || (defined('HHVM_VERSION_ID') && HHVM_VERSION_ID < 31100)) {
// There is NO support for escape param
fputcsv($handler, array($payload), ',', '"');
} else {
// There is support for escape param
fputcsv($handler, array($payload), ',', '"', '"');
}
rewind($handler);
$actual = rtrim(fgets($handler, 4096), "\n");
echo "There is no way to write a well formatted CSV even. The support for the escape param\n";
echo " in the fputcsv function do not fix the invalid format.\n";
echo sprintf("Expected: %s\n", $rfcCsv);
echo sprintf("Actual : %s\n", $actual);
echo "Equals? "; var_dump($rfcCsv === $actual);
echo "\n\n================================================================================\n\n";
$actualPhp = str_getcsv($actual, ',', '"', '"');
echo "READING PHP CSV\n";
echo "---------------\n";
echo "If you read a PHP formatted CSV using use the `double-quote` char as enclosure and\n";
echo " as escape, the result is not the expected one.\n";
echo sprintf("Expected: %s\n", $payload);
echo sprintf("Actual : %s\n", $actualPhp[0]);
echo "Equals? "; var_dump($payload === $actualPhp[0]);
echo "\n\n================================================================================\n\n";
READING RFC CSV
---------------
To read a well formatted CSV, you must use the `double-quote` char as enclosure and
as escape
Expected: "Hello\", World!
Actual : "Hello\", World!
Equals? bool(true)
================================================================================
WRITING
-------
There is no way to write a well formatted CSV even. The support for the escape param
in the fputcsv function do not fix the invalid format.
Expected: """Hello\"", World!"
Actual : ""Hello\", World!"
Equals? bool(false)
================================================================================
READING PHP CSV
---------------
If you read a PHP formatted CSV using use the `double-quote` char as enclosure and
as escape, the result is not the expected one.
Expected: "Hello\", World!
Actual : Hello\"
Equals? bool(false)
================================================================================
READING RFC CSV
---------------
To read a well formatted CSV, you must use the `double-quote` char as enclosure and
as escape
Expected: "Hello\", World!
Actual : "Hello\", World!
Equals? bool(true)
================================================================================
WRITING
-------
There is no way to write a well formatted CSV even. The support for the escape param
in the fputcsv function do not fix the invalid format.
Expected: """Hello\"", World!"
Actual : """Hello\", World!"
Equals? bool(false)
================================================================================
READING PHP CSV
---------------
If you read a PHP formatted CSV using use the `double-quote` char as enclosure and
as escape, the result is not the expected one.
Expected: "Hello\", World!
Actual : "Hello\
Equals? bool(false)
================================================================================