note 108188 modified in function.fgetcsv by sp

From: Date: Mon, 16 Apr 2012 16:20:53 +0000
Subject: note 108188 modified in function.fgetcsv by sp
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187181@lists.php.net to get a copy of this message
Here\&#039;s my class for CSV parsing. Note that it reads the delimiter. Class : &lt;?php /** * Simple CSV reading class from PHP5. * * The GNU License. * * Copyright (c) 2012 Jean-Marie Comets * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * @author Jean-Marie Comets &lt;jean.marie.comets@gmail.com&gt; * @version 1.0 * * @usage : * $csv = new Csv_Reader(\&#039;foo.bar\&#039;); // instances class * echo $csv-&gt;dump(); // returns data (raw HTML) * var_dump($csv-&gt;get()); // returns data (2D array) */ class Csv_Reader { private $file; private $delimiter; private $data; public function __construct($filename, $delimiter = null) // constructor { // load all lines of CSV file $this-&gt;file = file($filename); // if delimiter is not given, find it yourself $this-&gt;delimiter = ($delimiter === null) ? $this-&gt;delim() : $delimiter; // parse data with delimiter $this-&gt;parse(); } public function __destruct() // destructor { // not much to do as a matter of a fact =) } private function delim() // used to initialize $this-&gt;delimiter { // specify allowed field delimiters and order by priority $delimiters = array( \&#039;comma\&#039; =&gt; \&#039;,\&#039;, \&#039;semicolon\&#039; =&gt; \&#039;;\&#039;, \&#039;colon\&#039; =&gt; \&#039;:\&#039;, \&#039;pipe\&#039; =&gt; \&#039;|\&#039;, \&#039;tab\&#039; =&gt; \&#039;\\t\&#039; ); // compare for each delimiter average count of non-empty cells (long) $max_count = 0; $result = 0; // initialize foreach($delimiters as $index =&gt; $delimiter) { $average_count = 0; // initialize the average count foreach($this-&gt;file as $row) { // split with delimiter $tabRow = explode($delimiter,$row); // add count of non-empty cells $average_count += count(array_filter($tabRow)); } // calculate average $average_count /= count($this-&gt;file); // the average count is bigger than the previous if($average_count &gt; $max_count) { // change max average count $max_count = $average_count; // set result as current index $result = $index; } } // worst case scenario : returns the first delimiter which has the most priority return $delimiters[$result]; } private function parse() // parse data with set delimiter : initialize $this-&gt;data { // initialize data $this-&gt;data = array(); foreach($this-&gt;file as $row) { // parse row and put into $this-&gt;data $this-&gt;data[] = explode($this-&gt;delimiter,$row); } } public function get() // get parsed data (string * array) { return $this-&gt;data; // way to much to do here } public function dump() // dump as raw HTML table { $tbody = \&#039;&lt;tbody&gt;\&#039;; // start tbody foreach($this-&gt;data as $row) { $tbody .= \&#039;&lt;tr&gt;\&#039;; //beginning of new row foreach($row as $cell) { $tbody .= \&#039;&lt;td&gt;\&#039;.$cell.\&#039;&lt;/td&gt;\&#039;; // add cell } $tbody .= \&#039;&lt;/tr&gt;\&#039;; // end row } $tbody .= \&#039;&lt;/tbody&gt;\&#039;; // end tbody return \&#039;&lt;table&gt;\&#039;.$tbody.\&#039;&lt;/table&gt;\&#039;; // encapsulate in table tags } } --was-- Here's my class for CSV parsing. Note that it reads the delimiter. Class : <?php /** * Simple CSV reading class from PHP5. * * The GNU License. * * Copyright (c) 2012 Jean-Marie Comets * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * @author Jean-Marie Comets <jean.marie.comets@gmail.com> * @version 1.0 * * @usage : * $csv = new Csv_Reader('foo.bar'); // instances class * echo $csv->dump(); // returns data (raw HTML) * var_dump($csv->get()); // returns data (2D array) */ class Csv_Reader { private $file; private $delimiter; private $data; public function __construct($filename, $delimiter = null) // constructor { // load all lines of CSV file $this->file = file($filename); // if delimiter is not given, find it yourself $this->delimiter = ($delimiter === null) ? $this->delim() : $delimiter; // parse data with delimiter $this->parse(); } public function __destruct() // destructor { // not much to do as a matter of a fact =) } private function delim() // used to initialize $this->delimiter { // specify allowed field delimiters and order by priority $delimiters = array( 'comma' => ',', 'semicolon' => ';', 'colon' => ':', 'pipe' => '|', 'tab' => '\t' ); // compare for each delimiter average count of non-empty cells (long) $max_count = 0; $result = 0; // initialize foreach($delimiters as $index => $delimiter) { $average_count = 0; // initialize the average count foreach($this->file as $row) { // split with delimiter $tabRow = explode($delimiter,$row); // add count of non-empty cells $average_count += count(array_filter($tabRow)); } // calculate average $average_count /= count($this->file); // the average count is bigger than the previous if($average_count > $max_count) { // change max average count $max_count = $average_count; // set result as current index $result = $index; } } // worst case scenario : returns the first delimiter which has the most priority return $delimiters[$result]; } private function parse() // parse data with set delimiter : initialize $this->data { // initialize data $this->data = array(); foreach($this->file as $row) { // parse row and put into $this->data $this->data[] = explode($this->delimiter,$row); } } public function get() // get parsed data (string * array) { return $data; // way to much to do here } public function dump() // dump as raw HTML table { $tbody = '<tbody>'; // start tbody foreach($this->data as $row) { $tbody .= '<tr>'; //beginning of new row foreach($row as $cell) { $tbody .= '<td>'.$cell.'</td>'; // add cell } $tbody .= '</tr>'; // end row } $tbody .= '</tbody>'; // end tbody return '<table>'.$tbody.'</table>'; // encapsulate in table tags } } http://php.net/manual/en/function.fgetcsv.php

« previous php.notes (#187181) next »