![]() |
Importing comma-delimited csv file, containing commas
I'm working on learning how a particular script works. The script imports a comma-delimited text file, and outputs the contents to an HTML table.
The file contains a name field, i.e., "Doe, John" (with comma). The CSV files are comma-delimited, so I cannot use another character for the delimiter. Is there a solution to this? Thanks, Dave |
Quote:
|
Thanks! I'll try it, but I know I'll have a follow-up question!
|
Quote:
PHP Code:
|
Right. That's the problem.
The comma-delimited CSV file contains commas in the NAME field (e.g., "Doe, John"), but my delimiter in the program I'm studying is a "," (comma). Thus, when the file is read, it produces the following in an HTML table: PHP Code:
|
Just wanted to make sure I'm on the right path. So, I did a "search & replace" in the CSV file and changed the commas in the name field to the ASCII value.
The HTML table then displayed the CORRECTLY-FORMATTED information, so this works. However, is there a way to do this programmatically with with PHP as each line of the CSV file is read, so that I don't have to touch the CSV file? Would posting the code help? Thanks! Dave |
Can you give us a few lines of the CSV file showing the problem fields?
|
Here are a few lines below. Only 2 fields: a name field and an ID field. As you can see, the name field contains commas. When the program reads the CSV file, it generates a third field because it "thinks" that the comma within the name field is a field separator.
Thanks! PHP Code:
|
That data appears to be not comma-separated, but tab-separated. The commas shouldn't be an issue if you specify the correct delimiter (comma in CSV, tab in TSV, etc.).
PHP Code:
Code:
Array |
1 Attachment(s)
I'm a little confused. The tab delimiter is not working properly.
I'm attaching a csv file (mycsvfile.txt) which, I think, is comma-delimited. I'm still inappropriately getting 2 separate fields from the "name" field because it contains a comma. Thanks! |
That data is different to what you posted earlier and is indeed a normal CSV file. How are you parsing the data? fgetcsv should have no problem with it at all.
Does this work? PHP Code:
|
Yes, thanks very much. That works perfectly!
The script I was using to learn from used the fgets() function. Later in the script, the split() function was used, based on a comma delimiter. So, in looking back, maybe the author intended the script to be used for only comma-delimited text files, and not comma-delimited csv files. Although, I sorta thought that a comma-delimited text file WAS a csv file, but maybe not... Alright, well, this was a good learning experience! Thanks for the help. Dave |
A CSV file is technically a comma delimited text file but a simple split on commas does not adhere to the full CSV standard.
|
Thanks i was looking into the same problem i am having,, will try this and let you guys know.
|
| All times are GMT. The time now is 04:26 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0