Wrap CSV values generated by PHP fputcsv() with “ ”

后端 未结 5 1213
忘了有多久
忘了有多久 2020-12-19 01:41

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

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

5条回答
  •  一向
    一向 (楼主)
    2020-12-19 02:43

    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.

    
    

提交回复
热议问题