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')) return;
14 include_spip('inc/charsets');
17 * Based on an example by ramdac at ramdac dot org
18 * Returns a multi-dimensional array from a CSV file optionally using the
19 * first row as a header to create the underlying data as associative arrays.
20 * @param string $file Filepath including filename
21 * @param bool $head Use first row as header.
22 * @param string $delim Specify a delimiter other than a comma.
23 * @param int $len Line length to be passed to fgetcsv
24 * @return array or false on failure to retrieve any rows.
28 * Importer le charset d'une ligne
30 * @param unknown_type $texte
33 function importer_csv_importcharset($texte){
34 // le plus frequent, en particulier avec les trucs de ms@@@
35 $charset_source = 'iso-8859-1';
36 // mais open-office sait faire mieux, donc mefiance !
38 $charset_source = 'utf-8';
39 return importer_charset($texte,$charset_source);
43 * enlever les accents des cles presentes dans le head,
44 * sinon ca pose des problemes ...
49 function importer_csv_nettoie_key($key){
50 return translitteration($key);
51 /*$accents=array('�','�','�','�','�',"�","�","'");
52 $accents_rep=array('e','e','e','a','u',"o","c","_");
53 return str_replace($accents,$accents_rep,$key);*/
57 * Lit un fichier csv et retourne un tableau
58 * si $head est true, la premiere ligne est utilisee en header
59 * pour generer un tableau associatif
63 * @param string $delim
64 * @param string $enclos
68 function inc_importer_csv_dist($file, $head = false, $delim = ",", $enclos = '"', $len = 10000) {
70 if (@file_exists
($file)
71 AND $handle = fopen($file, "r")){
73 $header = fgetcsv($handle, $len, $delim, $enclos);
75 $header = array_map('importer_csv_importcharset',$header);
76 $header = array_map('importer_csv_nettoie_key',$header);
77 $header_type = array();
78 foreach ($header as $heading) {
79 if (!isset($header_type[$heading]))
80 $header_type[$heading] = "scalar";
82 $header_type[$heading] = "array";
86 while (($data = fgetcsv($handle, $len, $delim, $enclos)) !== FALSE) {
87 $data = array_map('importer_csv_importcharset',$data);
88 if ($head AND isset($header)) {
90 foreach ($header as $key=>$heading) {
91 if ($header_type[$heading]=="array"){
92 if (!isset($row[$heading]))
93 $row[$heading] = array();
94 if (isset($data[$key]) AND strlen($data[$key]))
95 $row[$heading][]= $data[$key];
98 $row[$heading]=(isset($data[$key])) ?
$data[$key] : '';