Bug->Req #72945 [Opn]: Allow to pass empty string as $escape to fgetcsv()
| From: | cmb@php.net | Date: | Fri, 26 Aug 2016 12:59:21 +0000 |
| Subject: | Bug->Req #72945 [Opn]: Allow to pass empty string as $escape to fgetcsv() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-203573@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72945&edit=1
ID: 72945
Updated by: cmb@php.net
Reported by: alex at shopblocks dot com
-Summary: CSV Cannot be read properly
+Summary: Allow to pass empty string as $escape to fgetcsv()
Status: Open
-Type: Bug
+Type: Feature/Change Request
-Package: *General Issues
+Package: Filesystem function related
Operating System: Mac OS X
PHP Version: 7.0.10
Block user comment: N
Private report: N
New Comment:
That behavior is caused by fgetcsv() catering for an escape
character (what is pretty much uncommon for CSV). The default
escape character is a backslash, so the attributes values which
contain only a backslash break the parser. You can easily work
around this issue by overriding the escape character to use a
character that isn't contained in the CSV, e.g.
fgetcsv($csv, 0, ',', '"', '|');
I'm changing this ticket to a feature request, because it would be
useful to be able to pass an empty string as $escape, so the
behavior of fgetcsv() would be more compliant to other CSV
implementations.
Previous Comments:
------------------------------------------------------------------------
[2016-08-26 09:16:38] alex at shopblocks dot com
Description:
------------
fgetcsv is not correctly getting all of the rows for a CSV file
Test CSV file:
https://gist.github.com/shopblocks/f5aadf8c9f6cc512471393d4053c66bb
As you can see, the file is valid and imports correctly into GitHub, OS X Numbers and Excel.
Test script:
---------------
<?php
function dd()
{
echo '<pre>';
array_map(function ($x) {
print_r($x);
}, func_get_args());
die(1);
}
$csv = fopen("test.csv", 'r');
$headers = fgetcsv($csv);
$row = fgetcsv($csv);
dd($headers, $row);
Expected result:
----------------
All 21 columns should be filled
Actual result:
--------------
only 15 columns get filled
Array
(
[0] => id
[1] => name
[2] => variants
[3] => sku
[4] => price_web
[5] => price_rrp
[6] => price_cost
[7] => quantity
[8] => visible
[9] => status
[10] => tax_id
[11] => images
[12] => price_prefix
[13] => price_modifier
[14] => text
[15] => product_video
[16] => tax_rate
[17] => attribute
[18] => attribute_value
[19] => manufacturer
[20] => category_id
)
Array
(
[0] => 1009
[1] => Halcyon Scout Light
[2] => Add a Boltsnap§90mm Stainless Steel Boltsnap§+4.0743
[3] => HalcyonScout
[4] => 121.6667
[5] => 121.667
[6] => 0.000
[7] => 1
[8] => 1
[9] => 0
[10] => 1
[11] => 9282§Halcyon_Scout_Light.jpg§Halcyon_Scout_Light.jpg
[12] => +
[13] => 4.0743
[14] =>
Several Halcyon products evolved out of necessity: the performance of available manufactured gear
was not acceptable under rigorous mission critical situations. The Scout is just such a product.
The Scout is machined out of a solid rod of delrin for strength and fitted with a proper voltage
bulb to reduce the risk of failure. Nearly all widely available reserve lights overdrive the bulb
to create a brighter beam. While overdriving the bulb is effective for short term use, it increases
the stress on the filament, resulting in a greater frequency of bulb failure.
The Scout has a machined attachment point at the rear of the light and is free from all the
plastic clips and other assorted snags typically present on mass manufactured lights.
Specifications:
Machined from a solid rod of delrin
5.5 watt bulb
Approx 3.5 hour burn time
Uses 3 x C cell batteries (not included)
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72945&edit=1