开发者

Wrap CSV values generated by PHP fputcsv() with " "

So, my code generates a CSV file using PHP's built-in fputcsv function.

开发者_Python百科

For the delimiter, I use ',' (a comma).

For the enclosure, I use '"' (a double-quote).

However, when I try something like

fputcsv($file,array('a','b',"long string, with commas",NULL,''),',','"');

it outputs

a,b,"long string, with commas",,

but I would like it to output

"a","b","long string, with commas","",""

Is there an easy way to deal with this, or would I have to write a replacement for fputcsv?


This is not usually a problem for CSV files.

fputcsv puts quotes around the value if it would be ambiguous. For example,

a,b,"long string, with commas",,

is not ambiguous, but,

a,b,long string, with commas,,

is, and will in most (read: all) cases be interpreted by the CSV reader as having more than 5 fields.

CSV parsers will accept string literals even without quotes around them.

If you want quotes around the values anyway, the following snippet would do that. It doesn't escape quotes inside the string - that exercise is left to the reader:

$row = '"' . implode('", "', $rowitems) . '"';

You would want to put this in a loop for all your rows.


I worked around this by inserting some bogus string characters, with a space, #@ @#, and then removing them. Here's a sample implementation:

//$exported is our array of data to export
$filename = 'myfile.csv';
$fp = fopen($filename, 'w');
foreach ($exported as $line => $row) {
    if ($line > 0) {
        foreach ($row as $key => $value) {
                $row[$key] = $value."#@ @#";
        }
    }
    fputcsv($fp, $row);
}

fclose($fp);
$contents = file_get_contents($filename);
$contents = str_replace("#@ @#", "", $contents);
file_put_contents($filename, $contents);

This encloses all fields in double quotes, including empty ones


I think solution will be like this,

$order_header_arr = array("Item1", "Item2","This is Item3");
fputcsv($fp, $order_header_arr,',',' ');

remember " "[Space] Between third parameter of fputcsv


Any reason you can't str_replace(',,',',"",',$output); ? You'd also have to see if the last or first character is a comma and if so, replace the comma with ,""


fputcsv will not enclose all array variables in quotes. Having a numeric array value without quotes may be correct but presents a problem when a label or address program encounters a numeric defined US zip code because it will strip the leading zeros when printing. Thus 05123-0019 becomes 5123-19.

To enclose all values, whether they exist or not, in quotes I read the input file with fgetsrc and write a corrected version using fwrite. fgetsrc reads the record into array variables. Since fwrite writes a variable, you must string the array variables, enclose them in quotes and separate the array variable with a comma. Then add the record separator.

<?php
// fgetcsv - read array with fgetcsv and string into output variable 
// then write with fwrite
// $ar is the array populated from fgetcsv and $arc is the variable strung 
// with array variables enclosed in quotes and written using fwrite.
$file_in = fopen("reinOCp.csv","r") or die("Unable to open input file 
reinOCp.csv!"); 
$file_out = fopen("printLABEL.csv", "w") or die("Unable to open output file 
prtLABEL!");
while (!feof($file_in)) {  //loop through each record of the input file
    $ar=fgetcsv($file_in); //read the record into array $ar   
    if (is_array($ar)){ //this loop will string all the array values plus 
// the end of record into variable $arc and then write the variable 
        $arc = ""; //clear variable $arc  
        foreach ($ar as $value) {      
            $arc .= '"' . $value . '",'; // add the array values, enclose in 
// quotes with comma separator and store in variable $arc
        }   
        $arc .= "\n"; //add end of record to variable $arc
        fwrite($file_out, $arc) or die ("ERROR: Cannot write the file"); 
//write the record using variable $arc
    }
}
echo "end of job";
fclose($file_in);
fclose($file_out);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜