PHP Classes

csv_bv: Retrieve data from CSV files (Ms Excel too)

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 56%Total: 3,154 All time: 1,140 This week: 50Up
Version Licenses PHP version Categories
csv_bv 1.0.0BSD License, GNU ...3Files and Folders
Description 

Author

This class can be used to parse and retrieve data from CSV files either in the standard or MS Excel format.

Two methods are provided to either process the file a line at a time or return the whole csv file as an array

It can:

- Handle line breaks within quoted fields
- Support different character separator (usually a comma or semicolon) in quoted fields
- Optionally remove leading and trailing white spaces and tabs
- Optionally skip empty rows
- Support Windows and Unix line breaks (care must be taken with Macintosh format)
- Remove escape characters

Picture of Ben Vautier
Name: Ben Vautier <contact>
Classes: 2 packages by
Country: Australia Australia
Age: 46
All time rank: 91114 in Australia Australia
Week rank: 163 Up2 in Australia Australia Up

Example

<?php



include_once 'class.csv_bv.php';

$csv = & new csv_bv('test.csv', ';', '"' , '\\'); // The last three fields are optional. If the last field is ommitted
                                                // the MS Excel standard is assumed, i.e. a double quote is used
                                                // to escape a double quote ("").
$csv->SkipEmptyRows(FALSE); // Will skip empty rows. TRUE by default. (Shown here for example only).
$csv->TrimFields(TRUE); // Remove leading and trailing \s and \t. TRUE by default.

while ($arr_data = $csv->NextLine()){

    echo
"<br><br>Processing line ". $csv->RowCount() . "<br>";
    echo
implode(' | ', $arr_data);

}

echo
"<br><br>Number of returned rows: ".$csv->RowCount();
echo
"<br><br>Number of skipped rows: ".$csv->SkippedRowCount();


//----
// OR using the csv2array function.
//----

include_once 'class.csv_bv.php';

$csv = & new csv_bv('test.csv', ';', '"' , '\\');
$csv->SkipEmptyRows(TRUE); // Will skip empty rows. TRUE by default. (Shown here for example only).
$csv->TrimFields(TRUE); // Remove leading and trailing \s and \t. TRUE by default.

$_arr = $csv->csv2Array();
print_r($_arr);

echo
"<br><br>Number of returned rows: ".$csv->RowCount();
echo
"<br><br>Number of skipped rows: ".$csv->SkippedRowCount();


?>


  Files folder image Files (3)  
File Role Description
Accessible without login Plain text file class.csv_bv.php Class main CSV parser class
Plain text file example.php Example Simple example
Plain text file test.csv Data csv test file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:3,154
This week:0
All time:1,140
This week:50Up
User Ratings User Comments (2)
 All time
Utility:80%StarStarStarStarStar
Consistency:85%StarStarStarStarStar
Documentation:-
Examples:80%StarStarStarStarStar
Tests:-
Videos:-
Overall:56%StarStarStar
Rank:1744
 
Excellent
14 years ago (kishore kumar)
70%StarStarStarStar
The code is very professionally commented.
16 years ago (Mike D.)
70%StarStarStarStar