Public Member Functions | |
__construct () | |
setLineLength ($length) | |
setDelimiter ($delimiter) | |
setEnclosure ($enclosure) | |
getData ($file) | |
getDataPairs ($file, $keyIndex=0, $valueIndex=1) | |
saveData ($file, $data) | |
fputcsv (&$handle, $fields=array(), $delimiter= ',', $enclosure= '"') | |
Protected Attributes | |
$_lineLength = 0 | |
$_delimiter = ',' | |
$_enclosure = '"' |
Definition at line 32 of file Csv.php.
fputcsv | ( | &$ | handle, | |
$ | fields = array() , |
|||
$ | delimiter = ',' , |
|||
$ | enclosure = '"' | |||
) |
Definition at line 137 of file Csv.php.
00137 { 00138 $str = ''; 00139 $escape_char = '\\'; 00140 foreach ($fields as $value) { 00141 if (strpos($value, $delimiter) !== false || 00142 strpos($value, $enclosure) !== false || 00143 strpos($value, "\n") !== false || 00144 strpos($value, "\r") !== false || 00145 strpos($value, "\t") !== false || 00146 strpos($value, ' ') !== false) { 00147 $str2 = $enclosure; 00148 $escaped = 0; 00149 $len = strlen($value); 00150 for ($i=0;$i<$len;$i++) { 00151 if ($value[$i] == $escape_char) { 00152 $escaped = 1; 00153 } else if (!$escaped && $value[$i] == $enclosure) { 00154 $str2 .= $enclosure; 00155 } else { 00156 $escaped = 0; 00157 } 00158 $str2 .= $value[$i]; 00159 } 00160 $str2 .= $enclosure; 00161 $str .= $str2.$delimiter; 00162 } else { 00163 $str .= $enclosure.$value.$enclosure.$delimiter; 00164 } 00165 } 00166 $str = substr($str,0,-1); 00167 $str .= "\n"; 00168 return fwrite($handle, $str); 00169 }
getData | ( | $ | file | ) |
Retrieve CSV file data as array
string | $file |
Definition at line 85 of file Csv.php.
00086 { 00087 $data = array(); 00088 if (!file_exists($file)) { 00089 throw new Exception('File "'.$file.'" do not exists'); 00090 } 00091 00092 $fh = fopen($file, 'r'); 00093 while ($rowData = fgetcsv($fh, $this->_lineLength, $this->_delimiter, $this->_enclosure)) { 00094 $data[] = $rowData; 00095 } 00096 fclose($fh); 00097 return $data; 00098 }
getDataPairs | ( | $ | file, | |
$ | keyIndex = 0 , |
|||
$ | valueIndex = 1 | |||
) |
Retrieve CSV file data as pairs
string | $file | |
int | $keyIndex | |
int | $valueIndex |
Reimplemented in Varien_File_Csv_Multy.
Definition at line 108 of file Csv.php.
00109 { 00110 $data = array(); 00111 $csvData = $this->getData($file); 00112 foreach ($csvData as $rowData) { 00113 if (isset($rowData[$keyIndex])) { 00114 $data[$rowData[$keyIndex]] = isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null; 00115 } 00116 } 00117 return $data; 00118 }
saveData | ( | $ | file, | |
$ | data | |||
) |
Saving data row array into file
string | $file | |
array | $data |
Definition at line 127 of file Csv.php.
00128 { 00129 $fh = fopen($file, 'w'); 00130 foreach ($data as $dataRow) { 00131 $this->fputcsv($fh, $dataRow, $this->_delimiter, $this->_enclosure); 00132 } 00133 fclose($fh); 00134 return $this; 00135 }
setDelimiter | ( | $ | delimiter | ) |
Set CSV column delimiter
string | $delimiter |
Definition at line 61 of file Csv.php.
setEnclosure | ( | $ | enclosure | ) |
Set CSV column value enclosure
string | $enclosure |
Definition at line 73 of file Csv.php.
setLineLength | ( | $ | length | ) |
Set max file line length
int | $length |
Definition at line 49 of file Csv.php.