roundcubemail/program/lib/Roundcube/rcube_csv2vcard.php
public function import($csv)
    {
        // convert to UTF-8
        $head      = substr($csv, 0, 4096);
        $charset   = rcube_charset::detect($head, RCUBE_CHARSET);
        $csv       = rcube_charset::convert($csv, $charset);
        $csv       = preg_replace(array('/^[\xFE\xFF]{2}/', '/^\xEF\xBB\xBF/', '/^\x00+/'), '', $csv); // also remove BOM
        $head      = '';
        $prev_line = false;

        $this->map       = array();
        $this->gmail_map = array();

        // Parse file
        foreach (preg_split("/[\r\n]+/", $csv) as $line) {
            if (!empty($prev_line)) {
                $line = '"' . $line;
            }

            $elements = $this->parse_line($line);

            if (empty($elements)) {
                continue;
            }

            // Parse header
            if (empty($this->map)) {
                $this->parse_header($elements);
                if (empty($this->map)) {
                    break;
                }
            }
            // Parse data row
            else {
                // handle multiline elements (e.g. Gmail)
                if (!empty($prev_line)) {
                    $first = array_shift($elements);

                    if ($first[0] == '"') {
                        $prev_line[count($prev_line)-1] = '"' . $prev_line[count($prev_line)-1] . "\n" . substr($first, 1);
                    }
                    else {
                        $prev_line[count($prev_line)-1] .= "\n" . $first;
                    }

                    $elements = array_merge($prev_line, $elements);
                }

                $last_element = $elements[count($elements)-1];
                if ($last_element[0] == '"') {
                    $elements[count($elements)-1] = substr($last_element, 1);
                    $prev_line = $elements;
                    continue;
                }
                $this->csv_to_vcard($elements);
                $prev_line = false;
            }
        }
    }
public function import($csv)
    {
        // convert to UTF-8
        $head      = substr($csv, 0, 4096);
        $charset   = rcube_charset::detect($head, RCUBE_CHARSET);
        $csv       = rcube_charset::convert($csv, $charset);
        $csv       = preg_replace(array('/^[\xFE\xFF]{2}/', '/^\xEF\xBB\xBF/', '/^\x00+/'), '', $csv); // also remove BOM
        $head      = '';
        $prev_line = false;

        $this->map       = array();
        $this->gmail_map = array();

        // Parse file
        foreach (preg_split("/[\r\n]+/", $csv) as $line) {
            if (!empty($prev_line)) {
                $line = '"' . $line;
            }

            $elements = $this->parse_line($line);

            if (empty($elements)) {
                continue;
            }

            // Parse header
            if (empty($this->map)) {
                $this->parse_header($elements);
                if (empty($this->map)) {
                    break;
                }
            }
            // Parse data row
            else {
                // handle multiline elements (e.g. Gmail)
                if (!empty($prev_line)) {
                    $first = array_shift($elements);

                    if ($first[0] == '"') {
                        $prev_line[count($prev_line)-1] = '"' . $prev_line[count($prev_line)-1] . "\n" . substr($first, 1);
                    }
                    else {
                        $prev_line[count($prev_line)-1] .= "\n" . $first;
                    }

                    $elements = array_merge($prev_line, $elements);
                }

                $last_element = $elements[count($elements)-1];
                if ($last_element[0] == '"') {
                    $elements[count($elements)-1] = substr($last_element, 1);
                    $prev_line = $elements;
                    continue;
                }
                $this->csv_to_vcard($elements);
                $prev_line = false;
            }
        }
    }
public function import ( $ csv ) { // convert to UTF-8 $ head = substr ( $ csv , 0 , 4096 ) ; $ charset = rcube_charset :: detect ( $ head , RCUBE_CHARSET ) ; $ csv = rcube_charset :: convert ( $ csv , $ charset ) ; $ csv = preg_replace ( array ( '/^[\xFE\xFF]{2}/' , '/^\xEF\xBB\xBF/' , '/^\x00+/' ) , '' , $ csv ) ; // also remove BOM $ head = '' ; $ prev_line = false ; $ this -> map = array ( ) ; $ this -> gmail_map = array ( ) ; // Parse file foreach ( preg_split ( "/[\r\n]+/" , $ csv ) as $ line ) { if ( ! empty ( $ prev_line ) ) { $ line = '"' . $ line ; } $ elements = $ this -> parse_line ( $ line ) ; if ( empty ( $ elements ) ) { continue ; } // Parse header if ( empty ( $ this -> map ) ) { $ this -> parse_header ( $ elements ) ; if ( empty ( $ this -> map ) ) { break ; } } // Parse data row else { // handle multiline elements (e.g. Gmail) if ( ! empty ( $ prev_line ) ) { $ first = array_shift ( $ elements ) ; if ( $ first [ 0 ] == '"' ) { $ prev_line [ count ( $ prev_line ) - 1 ] = '"' . $ prev_line [ count ( $ prev_line ) - 1 ] . "\n" . substr ( $ first , 1 ) ; } else { $ prev_line [ count ( $ prev_line ) - 1 ] .= "\n" . $ first ; } $ elements = array_merge ( $ prev_line , $ elements ) ; } $ last_element = $ elements [ count ( $ elements ) - 1 ] ; if ( $ last_element [ 0 ] == '"' ) { $ elements [ count ( $ elements ) - 1 ] = substr ( $ last_element , 1 ) ; $ prev_line = $ elements ; continue ; } $ this -> csv_to_vcard ( $ elements ) ; $ prev_line = false ; } } }
Import contacts from CSV file

@param string $csv Content of the CSV file
Import contacts from CSV file