Пример #1
0
 function toArrayOur($content, $fields = array(), $num_fields = 0, $params = '')
 {
     if (!$params) {
         $params = new DSCParameter();
     }
     $skip_first = $params->getValue('skip_first', true);
     $preserve_header = $params->getValu('preserve_header', false);
     $rec_deliminer = $params->getValue('rec_deliminer', "\n");
     $field_deliminer = $params->getValue('field_deliminer', ",");
     $clear_fields = $params->getValue('clear_fields', true);
     $preserve_indexes = $params->getValue('preserve_indexes', true);
     $num_records = $params->getValue('num_records', 0);
     $offset = $params->getValue('offset', 0);
     $begin_import = $params->getValue('begin_import', true);
     $chunk = $params->getValue('chunk_size', 4096);
     // 4kB by default
     $result = array();
     $tmp_lines = explode($rec_deliminer, $content);
     if (!$tmp_lines || !($c = count($tmp_lines))) {
         // no results or a deliminer is empty => empty array
         return $result;
     }
     if (!$num_fields) {
         // number of fields is not set => get it from header (firt line)
         $num_fields = count(explode($field_deliminer, $tmp_lines[0]));
     }
     $c = count($tmp_lines);
     // number of records
     if ($skip_first) {
         $tmp_head = array_shift($tmp_lines);
         if ($preserve_header) {
             // we want to preserve header
             $result[] = TiendaCSV::processFieldsToArray($fields, explode($field_deliminer, $tmp_head), $clear_fields, $preserve_indexes);
         }
         $c--;
         // adjust number of records
     }
     $record = 0;
     for ($i = 0; $i < $c; $i++) {
         if (!strlen($tmp_lines[$i])) {
             // skip empty lines between records
             continue;
         }
         $last_unclosed = false;
         $tmp_arr1 = array();
         $tmp_arr2 = array();
         $c_act = 0;
         while ($i < $c) {
             $tmp_arr2 = explode($field_deliminer, $tmp_lines[$i]);
             $c2 = count($tmp_arr2);
             $j = 0;
             if ($last_unclosed) {
                 // try to find a field with odd number of double quotes first
                 $tmp = array();
                 while ($j < $c2 && substr_count($tmp_arr2[$j], '"') % 2 != 1) {
                     $tmp[] = $tmp_arr2[$j++];
                 }
                 $tmp_arr1[$c_act] .= $rec_deliminer . implode($field_deliminer, $tmp);
                 // add them to the last field of previous line
             }
             if ($j == $c2) {
                 $last_unclosed = true;
                 $i++;
                 continue;
                 // continue to the next line
             } else {
                 if ($last_unclosed) {
                     $c_act++;
                     $last_unclosed = false;
                     $j++;
                 }
             }
             while ($j < $c2) {
                 if ($last_unclosed) {
                     $tmp = array();
                     $tmp[] = $tmp_arr2[$j++];
                     // first in the field is the current part
                     while ($j < $c2 && substr_count($tmp_arr2[$j], '"') % 2 != 1) {
                         // find another unclosed field
                         $tmp[] = $tmp_arr2[$j++];
                     }
                     if ($j < $c2) {
                         // if we  found the end -> save it
                         $tmp[] = $tmp_arr2[$j];
                     }
                     if (@strlen($tmp_arr1[$c_act])) {
                         // add this part to the rest of the current field
                         $tmp_arr1[$c_act] .= $row_deliminer . implode($field_deliminer, $tmp);
                     } else {
                         $tmp_arr1[$c_act] = implode($field_deliminer, $tmp);
                     }
                     // add the result to the current field
                     if ($j == $c2) {
                         // if we havent found any unclosed field until the end of this line, continue to the next line
                         continue;
                     }
                     // we found another unclosed field and matched it with the first one
                     $j++;
                     $last_unclosed = false;
                     $c_act++;
                     // continue to the next field
                 } else {
                     if (substr_count($tmp_arr2[$j], '"') % 2 == 1) {
                         $last_unclosed = true;
                         continue;
                     } else {
                         $last_unclosed = false;
                     }
                     // closed field => just copy it
                     @($tmp_arr1[$c_act++] = $tmp_arr2[$j++]);
                 }
             }
             if ($c_act == $num_fields) {
                 // we finished the record so we're good to go to parse a new record
                 break;
             }
             $i++;
             // otherwise, start parsing another line
         }
         $result[] = TiendaCSV::processFieldsToArray($fields, $tmp_arr1, $clear_fields, $preserve_indexes);
         $record++;
         if ($record == $num_records) {
             break;
         }
     }
     return $result;
 }