Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 15 revisions

[code] /**

  • CSVReader Class

  • @author Pierre-Jean Turpeau

  • @link http://www.codeigniter.com/wiki/CSVReader */ class CSVReader {

    var $fields;

    function parseText($p_Text, $p_Separator = ';') { $lines = explode("\n", $p_Text); return $this->parse($lines, $p_Separator); }

    function parse($p_CSVLines, $p_Separator = ';') {
    $content = FALSE; foreach ($p_CSVLines as $line_num => $line) { if( $line != '' ) { // skip empty lines $elements = split(';', $line);

             if( !is_array($content) ) { // the first line contains fields names
                 $this->_fields = $elements;
                 $content = array();
             } else {
                 $item = new stdClass();
                 $item->data = array();
                 foreach( $this->_fields as $id => $field ) {
                     if( $id == 'line' ) {
                         $item->line = $elements[$id];
                     } else {
                         $item->data[$field] = $elements[$id];
                     }
                 }
                 $content[] = $item;
             }
         }
     }
     return $content;
    

    } } ?> [/code]

Clone this wiki locally