Hi there.
I have some CSV data, quoted text, semi-colon delimited.
My format string is: csv|59|1|34
A line of text looks like this:
"A-5041";"15.980";"Bike Hook";"hang a bike";"4\"w x 6\"d x 11\"h";"granite";
The output:
Field 1: A-5041
Field 2: 15.980
Field 3: Bike Hook
Field 4: hang a bike
Field 5: 4\w x 6\d x 11\h
Field 6: granite
My quotes are gone in field 5. I'm not sure how to make this work.
Advice?
Thanks in advance,
Brian
Hello Brian,
Escaping quotes (which is what the \ is trying to do) is not standard practice within CSV files. The normal way to encode a quote within a quoted string is to include it twice.
However, in your case it should be easy enough to handle because of the semi-colon separation; so assuming that you would not expect the semi-colon character to appear within any of the fields you could drop the quote-delimited parameter from the format string, and then remove the leading and trailing quotes in each field manually within your record handler function.
In other words, use csv|59|1|0 as your format string; and then code similar to the following within your myRecordHandler function:
<?php
function myRecordHandler($record)
{
// trim leading and trailing quote characters from each field
// and fixup any escaped quotes within the fields
foreach($record as $key => $value)
{
$newValue = trim($value,'"');
$newValue = str_replace('\"','"',$newValue);
$record[$key] = $newValue;
}
// reset of record handler functionality here
}
?>
Hope this helps!
Cheers,
David.