Ignace Nyamagana Butera has a post has a post to his site showing how to use the LeagueCsv library for encapsulation in CSV output.
It is common knowledge that PHP’s fputcsv function does not allow enforcing the enclosure on every field. Using League CSV and PHP stream filter features let me show you how to do so step by step.
He walks you through the process of getting the library installed and using it (seven easy steps) to correctly contain the CSV values according to its contents:
- Install league csv
- Choose a sequence to enforce the presence of the enclosure character
- Set up you CSV
- Enforce the sequence on every CSV field
- Create a stream filter
- Attach the stream filter to the Writer object
Each step includes the code you'll need to make it work and a final result is shown at the end of the post. He does offer a few extra tips at the end of the post around some extra validation he added and where you can register the stream filter.