For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > November 2004 > CSV parsing









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author CSV parsing
Kevin Lin

2004-11-23, 3:56 pm

Hello all,

Anyone know of a way to call fgetcsv and fputcsv using a string rather than
a resource? (i.e.., parse a comma delimited string without writing it first
to a file.)

Thanks,
Kevin


Ken Robinson

2004-11-23, 3:56 pm


Kevin Lin wrote (in part):
> Anyone know of a way to call fgetcsv and fputcsv using a string

rather than
> a resource? (i.e.., parse a comma delimited string without writing

it first
> to a file.)


Use the explode() function:

<?
$str = explode(',','This,is,a,test');
?>

Results in the array $str with four elements:
$str[0] = 'This'
$str[1] = 'is'
$str[2] = 'a'
$str[3] = 'test'

Ken

Kevin Lin

2004-11-23, 3:56 pm

Thanks for the reply, but explode doesn't handle quoted cases, e.g.:

'Item1,"Item A,ItemB","Items ""one,"" ""two,"" and ""three"""'

should get parsed to:

$array[0]='Item1'
$array[1]='Item A,Item B'
$array[2]='Items "one," "two," and "three"'


"Ken Robinson" <kenrbnsn@rbnsn.com> wrote in message
news:1101230337.665372.261980@c13g2000cwb.googlegroups.com...
>
> Kevin Lin wrote (in part):
> rather than
> it first
>
> Use the explode() function:
>
> <?
> $str = explode(',','This,is,a,test');
> ?>
>
> Results in the array $str with four elements:
> $str[0] = 'This'
> $str[1] = 'is'
> $str[2] = 'a'
> $str[3] = 'test'
>
> Ken
>



Daniel Tryba

2004-11-23, 8:55 pm

Kevin Lin <kevin@wx3remove4spam.com> wrote:
> Thanks for the reply, but explode doesn't handle quoted cases, e.g.:

[snip]

By reading the user comments on the fgetcsv manual page one can find
http://bu.orbitel.bg/fgetcsvfromline.php. Seems the solution that you
are looking for.

Chung Leong

2004-11-24, 3:56 am


"Kevin Lin" <kevin@wx3REMOVE4SPAM.com> wrote in message
news:hdKod.557418$mD.522470@attbi_s02...
> Hello all,
>
> Anyone know of a way to call fgetcsv and fputcsv using a string rather

than
> a resource? (i.e.., parse a comma delimited string without writing it

first
> to a file.)
>
> Thanks,
> Kevin
>
>


Use the example class at
http://us2.php.net/manual/en/functi...er-register.php


R. Rajesh Jeba Anbiah

2004-11-24, 3:56 am

"Kevin Lin" <kevin@wx3REMOVE4SPAM.com> wrote in message news:<hdKod.557418$mD.522470@attbi_s02>...
> Hello all,
>
> Anyone know of a way to call fgetcsv and fputcsv using a string rather than
> a resource? (i.e.., parse a comma delimited string without writing it first
> to a file.)


IMHO, the most dependable way is to use existing fgetcsv along with
a temporary file:

<?php
function ParseCSVString($str, $delim=',')
{
//write the CSV string to a temporary file so that fgetcsv() can be
used to process...
$fp = tmpfile();
fwrite($fp, $str);
rewind($fp); //rewind to process CSV
$csv_length = strlen($str);
$data_arr = fgetcsv($fp, $csv_length, $delim);
fclose($fp); //clean up temp file
return $data_arr; //return the array
}
?>

Or may use csv_explode() found at <http://in.php.net/explode#37004>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com
Kevin Lin

2004-11-25, 3:55 am

This looks useful, thanks.

"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:bKCdnWRHkOBBdT7cRVn-tg@comcast.com...
>
> "Kevin Lin" <kevin@wx3REMOVE4SPAM.com> wrote in message
> news:hdKod.557418$mD.522470@attbi_s02...
> than
> first
>
> Use the example class at
> http://us2.php.net/manual/en/functi...er-register.php
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com