Preview: Csvreader.php
Size: 1.74 KB
/home/jambtst2015/public_html/protected/extensions/Csvreader.php
<?php
/**
* CCsvreader class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
class Csvreader extends CController
{
var $fields; /** columns names retrieved after parsing */
var $separator = ','; /** separator used to explode each line */
var $enclosure = '"'; /** enclosure used to decorate each field */
var $max_row_size = 4096; /** maximum row size to be used for decoding */
public static function parse_file($p_Filepath, $p_NamedFields = true) {
$content = false;
$file = fopen($p_Filepath, 'r');
if($p_NamedFields) {
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
}
while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {
if( $row[0] != null ) { // skip empty lines
if( !$content ) {
$content = array();
}
if( $p_NamedFields ) {
$items = array();
// I prefer to fill the array with values of defined fields
foreach( $this->fields as $id => $field ) {
if( isset($row[$id]) ) {
$field = str_replace(" ","_",strtolower($field));
$items[$field] = trim($row[$id]);
}
}
$content[] = $items;
} else {
$content[] = $row;
}
}
}
fclose($file);
return $content;
}
}
Directory Contents
Dirs: 2 × Files: 7