$value) { foreach ($tmp as $k => $v) { if ($key == $k) { $line[] = '"' . str_replace('"', '""', strip_tags($v)) . '"'; } } } } if ($tri_column && count($tri_column) == count($line)) { return implode(",", $line); } elseif (count($arr) == count($line)) { return implode(",", $line); } else throw new Exception('CSV failed'); } public static function arr_to_csv($arr, $tri_column = false) { $lines = array(); if ($tri_column) { $title = ""; foreach ($tri_column as $v) { if (isset($v['title'])) $title .= ( empty($title) ? "" : ",") . '"' . str_replace('"', '""', strip_tags($v['title'])) . '"'; } ! empty($title) ? $lines[] = $title : ""; } foreach ($arr as $v) { $lines[] = self::arr_to_csv_line($v, $tri_column); } return implode("\n", $lines); } public static function csv_to_arr($filename) { $separateur = ","; // For mac ini_set("auto_detect_line_endings", true); if ($FILE = fopen($filename, "r")) { $test1 = fgetcsv($FILE, 1024, ","); rewind($FILE); $test2 = fgetcsv($FILE, 1024, ";"); rewind($FILE); if (count($test1) == 1 || ( count($test2) > count($test1) && count($test2) < 20)) $separateur = ";"; while ($ARRAY[] = fgetcsv($FILE, 1024, $separateur)); fclose($FILE); array_pop($ARRAY); return $ARRAY; } } }