33fde21a087ae7f953d2d7209c4d5009aacadf2e
[lhc/web/www.git] / www / ecrire / inc / importer_csv.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
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 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) {
14 return;
15 }
16
17 include_spip('inc/charsets');
18
19 /**
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.
23 *
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.
29 */
30
31 /**
32 * Importer le charset d'une ligne
33 *
34 * @param string $texte
35 * @return array
36 */
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';
43 }
44
45 return importer_charset($texte, $charset_source);
46 }
47
48 /**
49 * enlever les accents des cles presentes dans le head,
50 * sinon ca pose des problemes ...
51 *
52 * @param string $key
53 * @return string
54 */
55 function importer_csv_nettoie_key($key) {
56 return translitteration($key);
57 }
58
59 /**
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
63 *
64 * @param string $file
65 * @param bool $head
66 * @param string $delim
67 * @param string $enclos
68 * @param int $len
69 * @return array
70 */
71 function inc_importer_csv_dist($file, $head = false, $delim = ", ", $enclos = '"', $len = 10000) {
72 $return = false;
73 if (@file_exists($file)
74 and $handle = fopen($file, "r")
75 ) {
76 if ($head) {
77 $header = fgetcsv($handle, $len, $delim, $enclos);
78 if ($header) {
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";
85 } else {
86 $header_type[$heading] = "array";
87 }
88 }
89 }
90 }
91 while (($data = fgetcsv($handle, $len, $delim, $enclos)) !== false) {
92 $data = array_map('importer_csv_importcharset', $data);
93 if ($head and isset($header)) {
94 $row = array();
95 foreach ($header as $key => $heading) {
96 if ($header_type[$heading] == "array") {
97 if (!isset($row[$heading])) {
98 $row[$heading] = array();
99 }
100 if (isset($data[$key]) and strlen($data[$key])) {
101 $row[$heading][] = $data[$key];
102 }
103 } else {
104 $row[$heading] = (isset($data[$key])) ? $data[$key] : '';
105 }
106 }
107 $return[] = $row;
108 } else {
109 $return[] = $data;
110 }
111 }
112 }
113
114 return $return;
115 }