Connecting Tech Pros Worldwide Help | Site Map

fgetcsv delimiters

Stephen Preston
Guest
 
Posts: n/a
#1: Mar 11 '06
I have an xls worksheet I wish to export as a text or csv file to import
with fgetcsv()

The 'save as' function on excell lets me save as a comma separated value or
tab delimited text files.
Trouble is, some of my cells already have commas in them, so a tab or
something else would be better.
In the delimiter option of fgcsv(), can you specify a tab and how?
My other option (but a bit of a chore) is to copy and paste the xls data
into word, then replace all the tabs with something like ; that dosn't
appear in the cells.

thanks


NC
Guest
 
Posts: n/a
#2: Mar 11 '06

re: fgetcsv delimiters


Stephen Preston wrote:[color=blue]
>
> I have an xls worksheet I wish to export as a text or csv file
> to import with fgetcsv()[/color]

So go ahead and do it; there shouldn't be any problems...
[color=blue]
> The 'save as' function on excell lets me save as a comma
> separated value or tab delimited text files.
> Trouble is, some of my cells already have commas in them[/color]

Doesn't matter; Excel encloses fields with commas in them in double
duotes, like this:

A simple field,"A field with a comma (,) in it",Another simple field

fgetcsv() understands this as well...
[color=blue]
> a tab or something else would be better.[/color]

This is a matter of opinion... :)
[color=blue]
> In the delimiter option of fgcsv(), can you specify a tab[/color]

You can if you want to:

fgetcsv($handle, 10240, "\t");

Note that since a custom delimiter is the third argument, the second
argument (the maximum length of a CSV string) must be specified...

For more information, see documentation on fgetcsv():

http://www.php.net/fgetcsv

Cheers,
NC

John Dunlop
Guest
 
Posts: n/a
#3: Mar 11 '06

re: fgetcsv delimiters


Stephen Preston:
[color=blue]
> Trouble is, some of my cells already have commas in them, so a tab or
> something else would be better.[/color]

No, that doesn't follow. You can hide your delimiter (commas by
default) by double quoting them (double quotes by default, but any
character you set the fourth argument to).
[color=blue]
> In the delimiter option of fgcsv(), can you specify a tab and how?[/color]

\t or however you insert tabs (got a tab key?).
[color=blue]
> My other option (but a bit of a chore) is to copy and paste the xls data
> into word, then replace all the tabs with something like ; that dosn't
> appear in the cells.[/color]

Well, I was about to say that TSV has the advantage that it boasts
its own registered MIME type, text/tab-separated-values; but lo and
behold text/csv was registered in October 2005.

http://www.ietf.org/rfc/rfc4180.txt

--
Jock

Closed Thread