2ca3e5627607dee3774cb7f7e666714da5829716
4 * Le plugin qui lave plus SPIP que SPIP
5 * (c) 2008 Mathieu Marcillaud, Cedric Morin, Tetue
8 * Fonctions de lecture d'un fichier CSV pour transformation en array()
12 if (!defined('_ECRIRE_INC_VERSION')) {
16 include_spip('inc/charsets');
19 * Based on an example by ramdac at ramdac dot org
20 * Returns a multi-dimensional array from a CSV file optionally using the
21 * first row as a header to create the underlying data as associative arrays.
22 * @param string $file Filepath including filename
23 * @param bool $head Use first row as header.
24 * @param string $delim Specify a delimiter other than a comma.
25 * @param int $len Line length to be passed to fgetcsv
26 * @return array or false on failure to retrieve any rows.
30 * Importer le charset d'une ligne
32 * Importe un texte de CSV dans un charset et le passe dans le charset du site (utf8 probablement)
33 * Les CSV peuvent sous ms@@@ avoir le charset 'iso-8859-1', mais pasfois aussi 'windows-1252' :/
36 * @param bool|string $definir_charset_source
37 * false : ne fait rien
38 * string : utilisera pour les prochains imports le charset indiqué
39 * true : remet le charset d'import par défaut de la fonction
42 function importer_csv_importcharset($texte, $definir_charset_source = false) {
43 // le plus frequent, en particulier avec les trucs de ms@@@
44 static $charset_source = 'iso-8859-1';
45 if ($definir_charset_source) {
46 if ($definir_charset_source === true) {
47 $charset_source = 'iso-8859-1';
49 $charset_source = $definir_charset_source;
52 // mais open-office sait faire mieux, donc mefiance !
53 if (is_utf8($texte)) {
56 $charset = $charset_source;
58 return importer_charset($texte, $charset);
62 * enlever les accents des cles presentes dans le head,
63 * sinon ca pose des problemes ...
68 function importer_csv_nettoie_key($key) {
69 return translitteration($key);
70 /*$accents=array('�','�','�','�','�',"�","�","'");
71 $accents_rep=array('e','e','e','a','u',"o","c","_");
72 return str_replace($accents,$accents_rep,$key);*/
76 * Lit un fichier csv et retourne un tableau
77 * si $head est true, la premiere ligne est utilisee en header
78 * pour generer un tableau associatif
82 * @param string $delim
83 * @param string $enclos
85 * @param string $charset_source
86 * Permet de définir un charset source du CSV, si différent de utf-8 ou iso-8859-1
89 function inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"', $len = 10000, $charset_source = '') {
91 if (@file_exists
($file)
92 and $handle = fopen($file, 'r')) {
93 if ($charset_source) {
94 importer_csv_importcharset('', $charset_source);
97 $header = fgetcsv($handle, $len, $delim, $enclos);
99 $header = array_map('importer_csv_importcharset', $header);
100 $header = array_map('importer_csv_nettoie_key', $header);
101 $header_type = array();
102 foreach ($header as $heading) {
103 if (!isset($header_type[$heading])) {
104 $header_type[$heading] = 'scalar';
106 $header_type[$heading] = 'array';
111 while (($data = fgetcsv($handle, $len, $delim, $enclos)) !== false) {
112 $data = array_map('importer_csv_importcharset', $data);
113 if ($head and isset($header)) {
115 foreach ($header as $key => $heading) {
116 if ($header_type[$heading] == 'array') {
117 if (!isset($row[$heading])) {
118 $row[$heading] = array();
120 if (isset($data[$key]) and strlen($data[$key])) {
121 $row[$heading][]= $data[$key];
124 $row[$heading]=(isset($data[$key])) ?
$data[$key] : '';
132 if ($charset_source) {
133 importer_csv_importcharset('', true);