PHP Classes
elePHPant
Icontem

PHP CSV File Parser: Filter and parse files in CSV or similar formats

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-02-14 (14 hours ago) RSS 2.0 feedStarStarStarStar 66%Total: 268 This week: 20All time: 7,438 This week: 30Up
Version License PHP version Categories
file-parser 1.0.3Free for non-comm...5Files and Folders, Text processing
Description Author

This class can filter and parse files in CSV or similar formats.

It can read a given file in CSV, TSV, or other formats with values separated by delimiter characters.

The class can convert the text encoding of the values and filter line record values using a callback function.

The values can be returned as objects with column properties set to given names.

Individual column values can be processed with a given callback function.

  Performance   Level  
Name: Nuno Chaves <contact>
Classes: 4 packages by
Country: Portugal Portugal

Details

jupitern/file-parser

PHP File Parser.

read, filter, parse and format {csv, tsv, dsv, variable-length-delimited} and other txt files

Requirements

PHP 5.4 or higher.

Installation

Include jupitern/file-parser in your project, by adding it to your composer.json file.

{
    "require": {
        "jupitern/file-parser": "1.*"
    }
}

Usage


Given a csv file "filename.csv" with contents (animal, category, count):
animal,type,count
crocodile,reptile,4
dolphin,mammal,0
duck,bird,2
koala,mammal,4
lion,mammal,5

lets parse the file with:
    - ignore the first line
    - convert encoding to ISO-9959-1
    - convert lines to objects
    - remove animals with count 0
    - format the animal type to uppercase
    - group by type

// read a file to array
$objectsArr = \Jupitern\Parser\FileParser::instance()
    ->setFile("csv.txt", ',')
    ->setEncoding('ISO-8859-1', 'UTF-8')
    ->toObject(['animal', 'type', 'animalCount'])
    ->filter(function ($line, $lineNumber) {
        return $lineNumber > 1 && $line->animalCounnt > 0;
    })
    ->format('type', function ($val) {
        return strtoupper($val);
    })
    ->group(function ($line) {
        return $line->type;
    })
    ->parse();

echo '<pre>';
print_r($objectsArr);

/*
output:
Array
(
    [REPTILE] => Array(
            [0] => stdClass Object(
                    [animal] => crocodile
                    [type] => REPTILE
                    [number] => 4
                )
        )

    [BIRD] => Array(
            [0] => stdClass Object(
                    [animal] => duck
                    [type] => BIRD
                    [number] => 2
                )
        )

    [MAMMAL] => Array (
            [0] => stdClass Object(
                    [animal] => koala
                    [type] => MAMMAL
                    [number] => 4
                )
            [1] => stdClass Object(
                    [animal] => lion
                    [type] => MAMMAL
                    [number] => 5
                )
        )

    [FISH] => Array
        (
            [0] => stdClass Object(
                    [animal] => áéíóú
                    [type] => FISH
                    [number] => 3
                )
        )
)
*/


For the same file lets parse with:
   - convert encoding to ISO-9959-1
   - convert lines to arrays
   - remove animals with count 0
   - group by type

$objectsArr = \Jupitern\Parser\FileParser::instance()
    ->setFile("csv.txt", ',')
    ->setEncoding('ISO-8859-1', 'UTF-8')
    ->filter(function ($line) {
        return $line[2] > 0;
    })
    ->group(function ($line) {
        return $line[1];
    })
    ->parse();

echo '<pre>';
print_r($objectsArr);

/*
Output:
Array
(
    [reptile] => Array
        (
            [0] => Array(
                    [0] => crocodile
                    [1] => reptile
                    [2] => 4
                )
        )
    [bird] => Array(
            [0] => Array(
                    [0] => duck
                    [1] => bird
                    [2] => 2
                )
        )
    [mammal] => Array
        (
            [0] => Array(
                    [0] => koala
                    [1] => mammal
                    [2] => 4
                )
            [1] => Array(
                    [0] => lion
                    [1] => mammal
                    [2] => 5
                )
        )
    [fish] => Array
        (
            [0] => Array(
                    [0] => áéíóú
                    [1] => fish
                    [2] => 3
                )
        )
)
*/


Given a dsv file "file.txt" with contents (empolyee number, birth date, monthly income):
01john doe        1980-01-01          923.5
01luis west       1976-01-01         1143.3
01madalena        1983-01-01         2173.6
02Jaqueline Wayne 1983-01-01         822.44
05luís manuel     1983-01-01        1323.52

lets parse the file doing:
    - convert encoding to ISO-9959-1
    - convert lines to objects
    - format person name capitalize first letters
    - group by wage bellow or above 1000

$objectsArr = \Jupitern\Parser\FileParser::instance()
    ->setFile("test.txt")
    ->setEncoding('ISO-8859-1', 'UTF-8')
    ->each(function ($line){
        $obj = [];
        $obj['Number'] = mb_substr($line, 0, 2);
        $obj['Name'] = mb_substr($line, 2, 16);
        $obj['BirthDate'] = mb_substr($line, 18, 10);
        $obj['MonthlyIncome'] = (float)mb_substr($line, 28, 15);
        return (object)$obj;
    })
    ->format('Name', function ($val) {
        return ucwords($val);
    })
    ->group(function ($line) {
        return (float)$line->MonthlyIncome >= 1000 ? 'above 1000' : 'bellow 1000';
    })
    ->parse();

echo '<pre>';
print_r($objectsArr);

/*
Output:
Array
(
    [bellow 1000] => Array(
            [0] => stdClass Object(
                    [Number] => 01
                    [Name] => John Doe
                    [BirthDate] => 1980-01-01
                    [MonthlyIncome] => 923.5
                )
            [1] => stdClass Object(
                    [Number] => 02
                    [Name] => Jaqueline Wayne
                    [BirthDate] => 1983-01-01
                    [MonthlyIncome] => 822.44
                )
        )
    [above 1000] => Array(
            [0] => stdClass Object(
                    [Number] => 01
                    [Name] => Luis West
                    [BirthDate] => 1976-01-01
                    [MonthlyIncome] => 1143.3
                )
            [1] => stdClass Object(
                    [Number] => 01
                    [Name] => Madalena
                    [BirthDate] => 1983-01-01
                    [MonthlyIncome] => 2173.6
                )
            [2] => stdClass Object(
                    [Number] => 05
                    [Name] => Luís Manuel
                    [BirthDate] => 1983-01-01
                    [MonthlyIncome] => 1323.52
                )
        )
)
*/

ChangeLog

- initial release

Contributing

- welcome to discuss a bugs, features and ideas.

License

jupitern/file-parser is release under the MIT license.

You are free to use, modify and distribute this software

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file FileParser.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:268
This week:20
All time:7,438
This week:30Up
 User Ratings  
 
 All time
Utility:93%StarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:100%StarStarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:66%StarStarStarStar
Rank:645