1

i am importing csv file to upload data into database. But in some products the description is not going through properly. the description is like

TSD/UHC Model UG-132, 6\" gas revolver with plastic shells. Shells: MUG131 & MUG131BRASS. 290-320 FPS with .20g BBS.

Legal Disclaimer

Restrictions: You must be 18 or older to order this product. In some areas, state and local laws further restrict or prohibit the sale and possession of this product. In ordering this product, you certify that you are at least 18 years old and satisfy your jurisdiction\'s legal requirements to purchase this product.

Warning: This product may be mistaken for a firearm by law enforcement officers or others, and altering its color or brandishing the product in public may be considered a crime.

but when i print fgetcsv array it display this description in different array like.

[2] => TSD/UHC Model UG-132, 6\" gas revolver with plastic shells. Shells: MUG131 & MUG131BRASS. 290-320 FPS with .20g BBS. Legal Disclaimer

Restrictions: You must be 18 or older to order this product. In some areas [3] => state and local laws further restrict or prohibit the sale and possession of this product. In ordering this product [4] => you certify that you are at least 18 years old and satisfy your jurisdiction\'s legal requirements to purchase this product.
Warning: This product may be mistaken for a firearm by law enforcement officers or others [5] => and altering its color or brandishing the product in public may be considered a crime.

"

I also have more products with this kind of description with backslash, single quot and double quot. some other products are uploaded properly but some are having problem.

Thanks

Kmeixner
  • 1,664
  • 4
  • 22
  • 32
Ritesh
  • 11
  • 2
  • Show the fgetcsv() input line that you are using so we can see what enclosures and escapes you're using – Mark Baker Jul 17 '13 at 10:59
  • i have mentioned text in question which i am parsing and also show that it splits into array so that i am facing problem in inserting data. String is too long so i cannot post it here so please refer it from question. it starts from "TSD/UHC Model UG-132, 6\" gas....." and ends with ".... public may be considered a crime.". Thanks for quick response Mark... – Ritesh Jul 17 '13 at 11:17
  • Add some code! Specifically where `fgetcsv` is called – tlenss Jul 17 '13 at 11:19
  • Please add that code line I've asked for; otherwise we can only guess that you're not correctly handling enclosures and escaped enclosures – Mark Baker Jul 17 '13 at 11:20
  • i have tried fgetcsv($file, 1000, "\t") and fgetcsv($file) and fgetcsv($file, 1000, ","). but still result same – Ritesh Jul 17 '13 at 11:24
  • Unless the whole entry is wrapped in quotes, then it won't matter what you try as it will be a malformed csv file (or even simply a plaintext file – Mark Baker Jul 17 '13 at 12:01

3 Answers3

1

fgetcsv($handle, 0, ",", '"', '""');

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
0

Try

fgetcsv($handle, 0, ",", '"', '\\');

which use a comma (,) as a separator, unless it's part of a field wrapped in "; and if " appears as a character in the field it must be escaped with a \

If a comma isn't the separator, then you'll need to use the appropriate separator character

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

I had a similar issue and tried many different ways but without preprocessing and cleaning the whole file this was the only working solution:

$handle = fopen($filepath, "r");
if ($handle === false) {
    throw new someException('fopen error');
}

$rowNumber = 1;
while (($data = fgetcsv($handle, 0, ',', '"', "\0")) !== false) {
    yield $rowNumber => $data;
    $rowNumber++;
}
fclose($handle);

I have found the working solution here: https://stackoverflow.com/a/46342634/5356216

More details with code and example data: https://stackoverflow.com/a/74721279/5356216

Zoltán Süle
  • 1,482
  • 19
  • 26