Get str_getcsv working on PHP 5.2

This commit is contained in:
Filippo Tessarotto 2012-11-24 09:42:09 +01:00
parent ddbce8e139
commit aaf5ca234c
1 changed files with 20 additions and 1 deletions

View File

@ -57,7 +57,7 @@ class testDataFileIterator implements Iterator
list($testData) = explode('//',$testDataRow); list($testData) = explode('//',$testDataRow);
// Split data into an array of individual values and a result // Split data into an array of individual values and a result
$dataSet = str_getcsv($testData,',',"'"); $dataSet = $this->_getcsv($testData, ',', "'");
foreach($dataSet as &$dataValue) { foreach($dataSet as &$dataValue) {
$dataValue = $this->_parseDataValue($dataValue); $dataValue = $this->_parseDataValue($dataValue);
} }
@ -66,6 +66,25 @@ class testDataFileIterator implements Iterator
return $dataSet; return $dataSet;
} }
private function _getcsv($input, $delimiter, $enclosure)
{
if (function_exists('str_getcsv')) {
return str_getcsv($input, $delimiter, $enclosure);
}
$temp = fopen('php://memory', 'rw');
fwrite($temp, $input);
rewind($temp);
$data = fgetcsv($temp, strlen($input), $delimiter, $enclosure);
fclose($temp);
if ($data === false) {
$data = array(null);
}
return $data;
}
private function _parseDataValue($dataValue) { private function _parseDataValue($dataValue) {
// discard any white space // discard any white space
$dataValue = trim($dataValue); $dataValue = trim($dataValue);