note 108188 added to function.fgetcsv
| From: | jean dot marie dot comets at gmail dot com | Date: | Thu, 05 Apr 2012 16:28:10 +0000 |
| Subject: | note 108188 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186986@lists.php.net to get a copy of this message | ||
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
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 134.214.163.192
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/108188
Del: integrated -- https://master.php.net/note/delete/108188/integrated
Del: useless -- https://master.php.net/note/delete/108188/useless
Del: bad code -- https://master.php.net/note/delete/108188/bad+code
Del: spam -- https://master.php.net/note/delete/108188/spam
Del: non-english -- https://master.php.net/note/delete/108188/non-english
Del: in docs -- https://master.php.net/note/delete/108188/in+docs
Del: other reasons-- https://master.php.net/note/delete/108188
Reject -- https://master.php.net/note/reject/108188
Search -- https://master.php.net/manage/user-notes.php