Skip to content
Advertisement

PHP Wrap ALL fields in quotes fputcsv()

I’m relatively new to PHP so please bear with me.

my code currently looks like this:

JavaScript

This works as expected, however, the client requires ALL fields to be wrapped in quotations, not just those containing spaces. I’ve seen a lot of solutions online but they all seem overly complicated or seem to suggest that I should edit the CSV once it’s exported.

If this is the case then I shall persevere but does anybody have a relatively simple solution for my dilemma?

Example

Normal code will output: col1,col2,"col 3",col4

I would like to output: "col1","col2","col 3","col4"

Any help would be much appreciated.

Thanks in advance, Paul

Advertisement

Answer

Try this for inspiration. You can replace the $row argument to fputcsv() with the array_map() line you see in my demo here:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement