goodby / csv

Goodby CSV is a high memory efficient flexible and extendable open-source CSV import/export library for PHP 5.3. 1. Memory Management Free This library designed for memory unbreakable. It will not be accumulated in the memory whole rows. The importer read CSV file and execute callback function line by line. 2. Multibyte support This library supports mulitbyte input/output: for example, SJIS-win, EUC-JP and UTF-8. 3. Ready to Use for Enterprise Applications Goodby CSV is fully unit-tested. The library is stable and ready to be used in large projects like enterprise applications.
MIT License
953 stars 147 forks source link

Is there a way to force enclosing certain (or all) fields? #34

Open Manc opened 10 years ago

Manc commented 10 years ago

As far as I know, by CSV standard you may enclose fields even if there is no real or obvious need.

In my case I'm dealing with a field for a German 5-digits postal code that may start with a zero (e.g. "01234"), but must always be interpreted as a string. Importing the CSV file with software such as Excel, the value is interpreted as number (1234) if it's not enclosed.

I tried a CallbackCollection and cast all values to string, but that didn't make any difference.

I could add a whitespace to numeric values, but then the CSV data contains whitespaces, which isn't great either.

Is there an option to force enclosing a field? Or at least a better trick than adding a whitespace?

Manc commented 10 years ago

Because I needed a quick and dirty solution I had to implement the CSV export myself. So I'm alright for now. If goodby/csv doesn't support force-enclosing values, I hereby suggest this as a new feature for a future version. Thanks!

jjeaton commented 9 years ago

:+1: for this

FadelChafai commented 9 years ago

Hi,

u can use this approach to deal with your problem

/\ $line is an array, each value is encapsulated by double quotes **/

private function addLine($line)
{
    $csv = implode(";", $line) . PHP_EOL;

    file_put_contents($this->_file, $csv, FILE_APPEND);
}