| | |
| | | |
| | | // Parse file |
| | | foreach (preg_split("/[\r\n]+/", $csv) as $i => $line) { |
| | | $line = trim($line); |
| | | if (empty($line)) { |
| | | continue; |
| | | } |
| | | |
| | | $elements = rcube_utils::explode_quoted_string(',', $line); |
| | | |
| | | $elements = $this->parse_line($line); |
| | | if (empty($elements)) { |
| | | continue; |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * Parse CSV file line |
| | | */ |
| | | protected function parse_line($line) |
| | | { |
| | | $line = trim($line); |
| | | if (empty($line)) { |
| | | return null; |
| | | } |
| | | |
| | | $fields = rcube_utils::explode_quoted_string(',', $line); |
| | | |
| | | // remove quotes if needed |
| | | if (!empty($fields)) { |
| | | foreach ($fields as $idx => $value) { |
| | | if (($len = strlen($value)) > 1 && $value[0] == '"' && $value[$len-1] == '"') { |
| | | // remove surrounding quotes |
| | | $value = substr($value, 1, -1); |
| | | // replace doubled quotes inside the string with single quote |
| | | $value = str_replace('""', '"', $value); |
| | | |
| | | $fields[$idx] = $value; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return $fields; |
| | | } |
| | | |
| | | /** |
| | | * Parse CSV header line, detect fields mapping |
| | | */ |
| | | protected function parse_header($elements) |