3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) {
17 include_spip('inc/charsets');
20 * Based on an example by ramdac at ramdac dot org
21 * Returns a multi-dimensional array from a CSV file optionally using the
22 * first row as a header to create the underlying data as associative arrays.
24 * @param string $file Filepath including filename
25 * @param bool $head Use first row as header.
26 * @param string $delim Specify a delimiter other than a comma.
27 * @param int $len Line length to be passed to fgetcsv
28 * @return array or false on failure to retrieve any rows.
32 * Importer le charset d'une ligne
34 * @param string $texte
37 function importer_csv_importcharset($texte) {
38 // le plus frequent, en particulier avec les trucs de ms@@@
39 $charset_source = 'iso-8859-1';
40 // mais open-office sait faire mieux, donc mefiance !
41 if (is_utf8($texte)) {
42 $charset_source = 'utf-8';
45 return importer_charset($texte, $charset_source);
49 * enlever les accents des cles presentes dans le head,
50 * sinon ca pose des problemes ...
55 function importer_csv_nettoie_key($key) {
56 return translitteration($key);
60 * Lit un fichier csv et retourne un tableau
61 * si $head est true, la premiere ligne est utilisee en header
62 * pour generer un tableau associatif
66 * @param string $delim
67 * @param string $enclos
71 function inc_importer_csv_dist($file, $head = false, $delim = ", ", $enclos = '"', $len = 10000) {
73 if (@file_exists
($file)
74 and $handle = fopen($file, "r")
77 $header = fgetcsv($handle, $len, $delim, $enclos);
79 $header = array_map('importer_csv_importcharset', $header);
80 $header = array_map('importer_csv_nettoie_key', $header);
81 $header_type = array();
82 foreach ($header as $heading) {
83 if (!isset($header_type[$heading])) {
84 $header_type[$heading] = "scalar";
86 $header_type[$heading] = "array";
91 while (($data = fgetcsv($handle, $len, $delim, $enclos)) !== false) {
92 $data = array_map('importer_csv_importcharset', $data);
93 if ($head and isset($header)) {
95 foreach ($header as $key => $heading) {
96 if ($header_type[$heading] == "array") {
97 if (!isset($row[$heading])) {
98 $row[$heading] = array();
100 if (isset($data[$key]) and strlen($data[$key])) {
101 $row[$heading][] = $data[$key];
104 $row[$heading] = (isset($data[$key])) ?
$data[$key] : '';