Don wrote:
Hi,
I have a CSV file, comma delimited with the data enclosed by double quotes.
I am using the fgetcsv() function to read and into an array and update a
database. It works great except for the odd record. After
investigating, I have ascertained that it is due to a backslash
character in the data which fgetcsv() cannot parse properly. I don;t
see anyway around this using fgetcsv(). Has anyone written a custom
routine for this?
Code Snippet
-------------------
$vvFile = 'myfile.csv';
$fph = fopen($vvFile,"r")
if ($fph) {
while (($data = fgetcsv($fph,4096,',','"')) !== FALSE) {M
// Insert fields from array '$data' to my MySQL database - will
fail on bad data
}
fclose($fph);
}
Sample Data
------------------
"123456","135679048754","7154904875","HD INDOOR INSECT KILR 33 OZ 6","EA"
"654321","246809052607","7154905260","59-2 CACTUS & SUCCULENTS \","EA"
Note: The first line is OK; the second will fail due to the backslash in
the fourth field
When I print the array contents, here is what I see. For the second
line, it is not parsing the row properly and joining the fourth and
fifth fields. When I edit and remove the backslash, all is OK.
first line:
data[0] = 123456
data[1] = 135679048754
data[2] = 7154904875
data[3] = HD INDOOR INSECT KILR 33 OZ 6
data[4] = EA
second line:
data[0] = 654321
data[1] = 246809052607
data[2] = 7154905260
data[3] = 59-2 CACTUS & SUCCULENTS ",EA
data[4] =
You can use stripslashes() on the data first.
Or, I generally use file_get_contents() and do my own parsing. That way I control the parsing.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php