wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.CSVUtil.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.CSVUtil.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/util/class.Message.php");
20 
21 /**
22  * @class CSVUtil
23  * @ingroup Util
24  * @brief CSVUtil provides basic support for csv file functionality.
25  * The first line of a csv file is supposed to hold the field names.
26  *
27  * @author ingo herwig <ingo@wemove.com>
28  */
29 class CSVUtil
30 {
31  var $_errorMsg = '';
32  var $_fields = array();
33 
34  /**
35  * Get last error message.
36  * @return The error string
37  */
38  function getErrorMsg()
39  {
40  return $this->_errorMsg;
41  }
42  /**
43  * Read a CSV file into an array
44  * @param filename The name of the CSV file
45  * @param separator The field separator used in the CSV file (e.g. \\t)
46  * @param fielddelimiter The field delimiter used in the CSV file (e.g. ")
47  * @result An assoziative array with keys 'fields', 'values' where
48  * values is an array of arrays holding the values
49  */
50  function readCSVFile($filename, $separator, $fielddelimiter)
51  {
52  $result = array();
53  $csv = fopen($filename, 'r');
54 
55  // get field definitions
56  $this->_fields = CSVUtil::getValues(fgets($csv, 4096), $separator, $fielddelimiter);
57  $result['fields'] = $this->_fields;
58  $result['values'] = array();
59 
60  // get values
61  while (!feof ($csv))
62  {
63  $line = fgets($csv, 4096);
64  if (strlen($line) > 0)
65  {
66  $values = CSVUtil::getValues($line, $separator, $fielddelimiter);
67  array_push($result['values'], $values);
68  }
69  }
70 
71  fclose ($csv);
72  return $result;
73  }
74  /**
75  * Get the values of of field from a line
76  * @param line The line (represented as array returned from readCSVFile)
77  * @param fieldName The name of the field
78  * @result The value or false if not existing
79  */
80  function getFieldValue($line, $fieldName)
81  {
82  if (in_array($fieldName, $this->_fields))
83  return $line[array_search($fieldName, $this->_fields)];
84  else
85  return false;
86  }
87  /**
88  * Get the values of of line
89  * @param line The line to split
90  * @param separator The field separator used in the CSV file (e.g. \\t)
91  * @param fielddelimiter The field delimiter used in the CSV file (e.g. ")
92  * @result An array of values
93  */
94  function getValues($line, $separator, $fielddelimiter)
95  {
96  $line = trim($line);
97  $values = preg_split("/".$separator."/", $line);
98 
99  // strip fielddelimiter from values
100  if (strlen($fielddelimiter) > 0)
101  for($i=0; $i<sizeof($values); $i++)
102  $values[$i] = trim($values[$i], $fielddelimiter);
103 
104  return $values;
105  }
106 }
107 ?>
readCSVFile($filename, $separator, $fielddelimiter)
getFieldValue($line, $fieldName)
CSVUtil provides basic support for csv file functionality. The first line of a csv file is supposed t...
getValues($line, $separator, $fielddelimiter)