Re: csv problem.. read csv from var

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2007/2/16, bedul <landavia81@xxxxxxxxx>:

i have problem with reading csv.. for i know the example script i get was
========================BASIC SCRIPT=================
$handle = fopen("hasillab.csv", "r");
$data2=$handle;
while($data = fgetcsv($handle, 1000, ",")){
foreach($data as $str)
  $data2.="<br>=  ".$str;

}

the result was:
==========================OUTPUT==========================
= A507257
= 3/2/2007
= Hematologi Lengkap,Cholesterol Total,LDL Cholesterol,Trigliserida,HDL
Cholesterol

hasillab.csv contain
A507257,3/2/2007,"Hematologi Lengkap,Cholesterol Total,LDL
Cholesterol,Trigliserida,HDL Cholesterol,Asam Urat,Gula Darah Puasa,Gula
Darah 2 Jam PP,Kreatinin,Ureum,Bilirubin Total,Alkali
Fosfatase,SGOT,SGPT,Urine Lengkap,Feses Rutin,Darah Samar Faeces,VDRL,Anti
-
HBs,Total PSA,HBsAg,Anti - HCV Total"

the problem i have is.. how about the csv is a var
========================DATA===============
$csv="
A507257,3/2/2007,\"Hematologi Lengkap,Cholesterol Total,LDL
Cholesterol,Trigliserida,HDL Cholesterol,Asam Urat,Gula Darah Puasa,Gula
Darah 2 Jam PP,Kreatinin,Ureum,Bilirubin Total,Alkali
Fosfatase,SGOT,SGPT,Urine Lengkap,Feses Rutin,Darah Samar Faeces,VDRL,Anti
-
HBs,Total PSA,HBsAg,Anti - HCV Total\"";

what should i do to make the ouput like above.

until now.. i try save the var into files then i use basic script to load
it

--
PHP Database Mailing List ( http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


You can parse the csv yourself. So, lets cast some regexp magic into the
emptyness of this mailing thread:

   preg_match_all('/(?<=^|,)("(.|\n)*(?<!")"|.*)(,|$)/Um', $csv, $matches,
PREG_SET_ORDER);
   $rows = array();
   $fields = array();
   foreach ($matches as $match) {
       $field = $match[1];
       if ($field != '' && $field != '"' && $field[0] == '"' &&
substr($field, -1) == '"') {
           $field = substr($field, 1, -1);
           $field = str_replace('""', '"', $field);
       }
       $fields[] = $field;
       if ($match[3] == '') {
           $rows[] = $fields;
           $fields = array();
       }
   }

$rows variable will be filled with an array for each row in a CSV format
complaint string, using line breaks as row separators and commas as field
separators.

If you wonder about the mystic forces that powers this regexp, I'll be more
than willing to explain them throughly, if time becomes available. If it
doesn't work (I tested it againts all the use cases I could think of, but...
murphy's law applies) go blame someone else... =P

----

Also, if you're working on PHP5, you may bring up the new, mostly unused,
spells in your PHP spell book, available to all those who have reached level
5 in PHP wizardry. Just copy & paste (that skill you have probably adquired
in your early days as an apprentice) the code from the PHP manual included
as an example of custom stream wrappers:

http://www.php.net/stream_wrapper_register

With the VariableStream class registered as a stream wrapper, you can
magically use global variables as a streams. Therefore something like this
will work:

$csv="...etc...";
$handle = fopen("var://csv", "r");
... etc ...

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux