Date: 04/27/99
- Next message: Bug Database: "[PHP-DEV] Bug #1356 Updated: Not coherent hyperwave support"
- Previous message: bug <email protected>: "[PHP-DEV] Bug #1357: libphp3.so don't load"
- Next in thread: Nick Talbott: "[PHP-DEV] Re: [PHP3] CSV conversion. Inclusion in PHP?"
- Maybe reply: Nick Talbott: "[PHP-DEV] Re: [PHP3] CSV conversion. Inclusion in PHP?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi Nick,
Thanks for that that was exactly what I wanted.
Since I had little response yesterday I started to write my own script in PHP just so I could get the structure of the code correct.
I got it working perfectly and with a bit of testing decided that it would need to be added either as a PHP internal function or external c function.
Looking at your code it's prety much the same as the way I did it except I never got to convert it into C code for inclusion into php.
One thing I did note is that you didn't mention if you took into account CRLF within a quoted field?
I was told this was part of the CSV spec?
As for including it within PHP I ask the developers, I have looked at the manual on how to add functions to php.
How can we go about getting this put in the main PHP release?
If it is too difficult then another question which I would like to be able to do for other functions.
Can I have an externally compiled function which is called from PHP with something like "passthru" and have it return a 2 or 1 dimensional array? (I'm pretty sure it can't be done but there's no harm in asking)
Also if we can get this added to the PHP CVS I will try and write the trivial fputcsv( ) to write the a record to a CSV file. :)
>I did some work on this a few months ago and wrote a function in C called
>fgetcsv() which does just this.
>I sent it in to Rasmus, but I've not heard what happened to it.
>
>Here's a description of my function and how to use it.
>The code is attached to avoid problems with email messing with line ends!
>There's some brief notes on how to add the function to the PHP source. Of
>course, you'll need to rebuild PHP after you've done this.
>
>Hope this does the job for you.
>
>Regards - Nick
>
>Nick Talbott
>Corporate IT Unit, Powys County Council, UK
>
>email nickt <email protected>
>FAX +44 (0) 1597 824781
>web http://www.powys.gov.uk
>
>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
>Writing in CSV format from PHP is fairly trivial, but reading from them is
>not so straightforward because of the character by character processing
>that has to be done. This is pretty inefficient in an interpreted
>language anyway.
>
>I've wanted to handle CSV files in PHP before, so as a Christmas break
>exercise I decided to try and write a function to enable PHP to do it.
>Here's the result. It's my first attempt at writing a function for
>PHP, so please forgive any errors in implementation or style!
>
>The way I approached it was to add a function fgetcsv() to the file
>functions, as a derivative of fgetss(). Essentially what it does is to
>process a line of input and return the fields in an array. It is
>quite tolerant of variations in CSV file layout, including not being
>worried by extra leading or trailing whitespace around the fields.
>The source is heavily commented.
>
>The way fgetcsv() is written would also allow reading from files that
>use the extended CSV syntax to allow comments, table and fieldnames in
>files (see http://www.tor.ec.gc.ca/woudc/data/document/extCSV.htm )
>
>Reading a CSV file in PHP would therefore be similar to reading a plain
>text file:
>
>- fopen() the file
>- use fgetcsv() to read the data a line at a time
>- fclose() the file
>
>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
>fgetcsv() works with files that follow the following CSV rules:
>
>- fields are separated by the COMMA character
>- each line ends with CR and/or LF
>- if a field contains either a COMMA or DOUBLE-QUOTE character the
>entire field is enclosed in DOUBLE-QUOTE characters, which are
>removed during reading
>- DOUBLE-QUOTE characters within a field are represented by a sequence
>of two DOUBLE-QUOTEs
>- NULL fields are empty fields
>
>fgetcsv() will additionally remove any leading or trailing whitespace
>(spaces, tabs) surrounding non-quoted fields
>
>fgetcsv() is written so that the delimiter could be changed from a
>COMMA to any other character if required. The discussion on the PHP
>list suggested that the SEMI-COLON may be used instead of the COMMA in
>some countries. Very minor alterations would even allow this to be
>provided as an optional third parameter in the function call.
>If you'd like me to do this, let me know.
>
>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
>CSV file reading algorithm:
>
>Read line from file (same code as fgetss() )
>
>Strip trailing LF, CR and any other space characters
>
>Repeat
>
> - strip any leading whitespace
> - if first non-space is DOUBLE-QUOTE read to matching DOUBLE-QUOTE
> and to following COMMA or LINE-END, handling embedded
> DOUBLE-QUOTES
> - if first non-space is not DOUBLE-QUOTE read to following COMMA
> or LINE-END and remove any trailing whitespace
>- save field
>
> Until LINE-END
>
>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
>Possible PHP manual entry for fgetcsv():
>
>fgetcsv
>
>fgetcsv -- get line from file pointer and parse for CSV fields
>
>
>Description
>
>array fgetcsv(int fp, int length);
>
>Similar to fgets() except that fgetcsv() parses the line it reads for
>fields in CSV format and returns an array containing the fields read.
>
>fp must be a valid file pointer to a file successfully opened by
>fopen(), popen(), or fsockopen()
>
>length must be greater than the longest line to be found in the CSV
>file (allowing for trailing line-end characters).
>
>fgetcsv() returns false on error, including end of file.
>
>NB A blank line in a CSV file will be returned as an array comprising
>just one single null field, and will not be treated as an error.
>
>
>Example
>Read and print entire contents of a CSV file
>
>$row=1;
>$fp = fopen("test.csv","r");
>while ($data = fgetcsv($fp,1000)) {
>$num = count($data);
>print "<p> $num fields in line $row: <br>";
>$row++;
>for ( $c=0; $c<$num; $c++ ) print $data[$c] . "<br>";
>}
>fclose($fp);
>
-- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe <email protected> For help: php-dev-help <email protected>
- Next message: Bug Database: "[PHP-DEV] Bug #1356 Updated: Not coherent hyperwave support"
- Previous message: bug <email protected>: "[PHP-DEV] Bug #1357: libphp3.so don't load"
- Next in thread: Nick Talbott: "[PHP-DEV] Re: [PHP3] CSV conversion. Inclusion in PHP?"
- Maybe reply: Nick Talbott: "[PHP-DEV] Re: [PHP3] CSV conversion. Inclusion in PHP?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

