Questions tagged [fgetcsv]

fgetcsv() is a PHP function which parses the line it reads for fields in CSV format and returns an array containing the fields read.

491 questions
294
votes
6 answers

How to properly escape a double quote in CSV?

I have a line like this in my CSV: "Samsung U600 24"","10000003409","1","10000003427" Quote next to 24 is used to express inches, while the quote just next to that quote closes the field. I'm reading the line with fgetcsv but the parser makes a…
srgb
  • 4,783
  • 6
  • 29
  • 45
185
votes
6 answers

How to parse a CSV file using PHP

Suppose I have a .csv file with the following content: "text, with commas","another text",123,"text",5; "some without commas","another text",123,"text"; "some text with commas or no",,123,"text"; How can I parse the content through PHP?
smith
  • 5,341
  • 8
  • 31
  • 38
65
votes
2 answers

fgetcsv fails to read line ending in mac formatted csv file, any better solution?

I was parsing a csv file using php with fgetcsv function. It parsed all content in a line, later i found, csv contains carraige return as "\r". I saw - it was reported as php bug before. I've solved this by setting php runtime configuration which is…
Musa
  • 3,944
  • 4
  • 21
  • 27
39
votes
6 answers

php fgetcsv returning all lines

I have the following csv file: upc/ean/isbn,item name,category,supplier id,cost price,unit price,tax 1 name,tax 1 percent,tax 2 name,tax 2 percent,quantity,reorder level,description,allow alt. description,item has serial number ,Apple…
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
38
votes
6 answers

UTF-8 problems while reading CSV file with fgetcsv

I try to read a CSV and echo the content. But the content displays the characters wrong. Mäx Müstermänn -> Mäx Müstermänn Encoding of the CSV file is UTF-8 without BOM (checked with Notepad++). This is the content of the CSV…
testing
  • 19,681
  • 50
  • 236
  • 417
33
votes
8 answers

PHP dynamically create CSV: Skip the first line of a CSV file

I am trying to import a CSV file. Due to the program we use, the first row is basically all headers that I would like to skip since I've already put my own headers in via HTML. How can I get the code to skip the first row of the CSV? (the strpos…
Nathan Dunn
  • 333
  • 1
  • 3
  • 5
27
votes
6 answers
17
votes
7 answers

Remove BOM () from imported .csv file

I want to delete the BOM from my imported file, but it just doesn't seem to work. I tried to preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $file); and a str_replace. I hope anybody sees what I'm doing wrong. $filepath =…
Interactive
  • 1,474
  • 5
  • 25
  • 57
15
votes
3 answers

fgetcsv skip blank lines in file

I have this script that I did, it basically grabs all the files in my "logs" folder and merge them all in one array file, my only problem is that, sometimes the script breaks if there is blank line or empty line! how can I tell it to automatically…
hyperexpert
  • 532
  • 1
  • 8
  • 20
12
votes
2 answers

CSVs without quotes not working with fgetcsv

I'm trying to parse CSV files uploaded by the user through PHP, but it's not working properly. I've uploaded several properly formatted CSVs and it worked fine, however; I have many users trying to import CSV files exported from Excel and they are…
Stephen Belanger
  • 6,251
  • 11
  • 45
  • 49
11
votes
4 answers

fgetcsv() ignores special characters when they are at the beginning of line!

I have a simple script that accepts a CSV file and reads every row into an array. I then cycle through each column of the first row (in my case it holds the questions of a survey) and I print them out. The survey is in french and whenever the first…
Gazillion
  • 4,822
  • 11
  • 42
  • 59
10
votes
2 answers

Fastest way to read a csv file

I'm looking for a very fast method to read a csv file. My data structure looks like this: timestamp ,float , string ,ip ,string 1318190061,1640851625, lore ipsum,84.169.42.48,appname and I'm using fgetcsv to read this data into…
Nyoman
  • 197
  • 1
  • 9
10
votes
4 answers

fgetcsv() not respecting newlines while converting .csv file to array

I have this array with airport codes and city names (around 3500 lines). code,city "Abilene, TX ",ABI "Adak Island, AK ",ADK "Akiachak, AK ",KKI "Akiak, AK ",AKI "Akron/Canton, OH ",CAK "Akuton, AK ",KQA "Alakanuk, AK ",AUK "Alamogordo, NM ",ALM I…
Andres SK
  • 10,779
  • 25
  • 90
  • 152
10
votes
2 answers

php fgetcsv and integer conversion (UTF8 x UCS-2 Little Endian)

UPDATE 4: I've found (as suspected) that the files enconding is responsible for this. I converted the csv file from UCS-2 Little Endian to UTF-8 and everything works fine. Anyway I need to find some way to make PHP understands the input file…
Paulo Bueno
  • 2,499
  • 6
  • 42
  • 68
9
votes
2 answers

fGetCsv only reading first line?

I'm trying to use fgetCsv but for some reason it is only reading the first line. Here is the code: $fieldseparator = ","; $lineseparator = "\r"; if(!file_exists($csvFile)) { echo "
Cannot find uploaded file. Please try…
THEK
  • 740
  • 2
  • 10
  • 29
1
2 3
32 33