|
Posted by Rik on 08/17/07 02:29
On Fri, 17 Aug 2007 04:06:46 +0200, Sanders Kaufman <bucky@kaufman.net> =
=
wrote:
> Rik wrote:
>
>> Hmmmz, someone posted an interesting solution:
>> function parseCSV($str, $delimiter =3D ',', $enclosure =3D '"', $len=
=3D 0)
>> {
>> $fh =3D fopen('php://memory', 'w+');
>> fwrite($fh, $str);
>> rewind($fh);
>> $result =3D fgetcsv( $fh, $len, $delimiter, $enclosure );
>> fclose($fh);
>> return $result;
>> }
>> var_dump(parseCSV('"foo","bar\"",234,324,"boz"'));
>
> Oh, I get it! PHP can parse a CSV *file*, but not a CSV *string*. So=
, =
> to parse the string, he just created a file in memory, and parsed that=
..
>
> That's cool. I'll bet there are other cool ways to make use of that =
> technique.
Yup, one of the main advantages is 'directing' output. Say for instance =
I =
have a logger class. I can set the output where to log in a single strin=
g, =
making it quite versatile. Log to the screen, a systemfile, a file on a =
=
ftpserver, to some socket; hell, even a database if I define a wrapper f=
or =
it, all possible with giving it a single target, and the same code logs =
to =
it without any problems.
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|