A PHP function to convert CSV strings to PHP arrays , but not a function for the other way around, so I wrote one:
function echocsv(array $arr, $quo = '"', $sep = ',') { $escape = $quo . $quo; foreach($arr as &$val) { if(strpos($val, $quo) !== false) { $val = $quo . str_replace($quo, $escape, $val) . $quo; } } echo implode($sep, $arr) . PHP_EOL; }
Is there anything that I'm missing? From wikipedia it is basically said that quotes should be escaped with another quote, and thatβs almost all there is to it. The .CSV file must be opened in MS Excel.
My primitive tests seem to suggest that they work.
(I repeat it, not return the string, because I'm going to pass it directly to the browser)
source share