function getCSVValues($string, $separator=",")
{
$elements = explode($separator, $string);
for ($i = 0; $i < count($elements); $i++)
{
$nquotes = substrcount($elements[$i], '"');
if ($nquotes %2 == 1)
{
for ($j = $i+1; $j < count($elements); $j++)
{
if (substrcount($elements[$j], '"') > 0)
{
arraysplice($elements, $i, $j-$i+1,
implode($separator, arrayslice($elements, $i, $j-$i+1)));
break;
}
}
}
if ($nquotes > 0)
{
$qstr = $elements[$i];
$qstr = substrreplace($qstr, '', strpos($qstr, '"'), 1);
$qstr = substrreplace($qstr, '', strrpos($qstr, '"'), 1);
$qstr = strreplace('""', '"', $qstr);
}
}
return $elements;
}
$file = filegetcontents('sample.csv');
$dataStrings = explode("\r", $file);
$i = 0;
foreach ( $dataStrings as $data ) $i++;
$files = "upload/YourFile.csv";
$handle = fopen($files, 'w');
$num = count($dataStrings);
echo $num;
for ( $j = 1; $j < $num; $j++ )
{
$strings = getCSVValues( $dataStrings[$j] );
$data = $strings.",";
fwrite($handle, $dataStrings[$j]);
echo "<pre>";
printr($strings);
print("<br/>\n\r");
echo "</pre>";
}
fclose($handle);