ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCSVReader.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
35 {
36  private $ptr_file = null;
37  private $data = array();
38  private $separator = ';';
39  private $delimiter = '""';
40  private $length = 1024;
41 
49  public function __construct()
50  {
51 
52  }
53 
54  /*
55  * Set Seperator
56  *
57  * @access public
58  * @param string field seperator
59  *
60  */
61  public function setSeparator($a_sep)
62  {
63  $this->separator = $a_sep;
64  }
65 
73  public function setDelimiter($a_del)
74  {
75  $this->delimiter = $a_del;
76  }
77 
85  public function setLength($a_length)
86  {
87  $this->length = $a_length;
88  }
89 
90  public function open($file = "")
91  {
92  return( $this->ptr_file = @fopen(ilUtil::stripSlashes($file), "r") );
93  }
94 
95  public function close()
96  {
97  return( @fclose($this->ptr_file) );
98  }
99 
107  public function getDataArrayFromCSVFile()
108  {
109  $row = 0;
110 
111  while (($line = fgetcsv($this->ptr_file, $this->length, $this->separator)) !== FALSE)
112  {
113  for ($col = 0; $col < count($line); $col++)
114  {
115  $this->data[$row][$col] = $this->unquote($line[$col]);
116  }
117 
118  ++$row;
119  }
120 
121  return $this->data;
122  }
123 
130  private function unquote($a_str)
131  {
132  return str_replace($this->delimiter.$this->delimiter, $this->delimiter,$a_str);
133  }
134 }
135 ?>