I have script which makes csv file from xls-file. But I need to get rid of quotes and decimals and also replace delimiter , to ; Original: 5.67710K,"1,00" 5.72160K,"-1,00" 5.75800K,"2,00" 5.81240K,"0,00" cleaned: 5.67710K;1 5.72160K;-1 5.75800K;2 5.81240K;0 How to do it? Code which cleans empty rows below. What to add there? if (false !== ($ih = fopen($input, 'r'))) { $oh = fopen($output, 'w'); while (false !== ($data = fgetcsv($ih, 80000, ","))) { if ($data[2] != NULL and $data[6] != NULL) { $outputData = array($data[2], $data[6]); fputcsv($oh, $outputData); } } fclose($ih); fclose($oh); } PHP:
I think you could use trim() for the double-quotes, then just concatenate strings with implode() ? And since the resulting string is no longer a valid CSV line, fputcsv() will not be the right one.
@hdewantara has it right that fputcsv is the wrong function, because what you seem to want for a result IS NOT CSV! Just fwrite it with the semi-colon and \r\n at the end. if ($ih = fopen($input, 'r')) { $oh = fopen($output, 'w'); while ($data = fgetcsv($ih, 65528)) { if ( array_key_exists(2, $data) && array_key_exists(6, $data) && !empty($data[2]) && !empty($data[6]) ) fwrite($oh, $data[2] . ';' . (int) $data[6] . "\r\n"); } fclose($ih); fclose($oh); } Code (markup): -- edit -- oh, and don't go so utterly batshit on the literal boolean comparisons. Waste of code... comma is the default delimiter, and I would REALLY worry if you have single lines in your CSV that break the 64k minus 64 bit pointer size mark... don't check for null as the access will be invalid and still thrown an error (this isn't JavaScript) that's why "array_key_exists" and "empty" exists, and you should be using logical "&&" not the higher precedence "and"
Thank you. Works great. I'm a newbie with these. My solutions are mainly taken from google search results