Download the PHP package nackjicholson/file-parser without Composer
On this page you can find all versions of the php package nackjicholson/file-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nackjicholson/file-parser
More information about nackjicholson/file-parser
Files in nackjicholson/file-parser
Package file-parser
Short Description PHP package for parsing data contained in a file into a php array.
License MIT
Informations about the package file-parser
file-parser
This is a composer package for parsing files that contain structured data.
The current supported formats are:
- Csv
- Json
- Yaml
- .ini
WARNING Only works with PHP 5.4 and up due to short array syntax and use of traits.
Example:
foobar.yml
example.php
Outputs:
Install
Via composer
compser require nackjicholson/file-parser=~2.1
or add too composer.json
CSV
This library provides three ways to parse a csv file into a php array. There is full support for delimiter, enclosure, and escape options by passing an associative array of options to each csv method. Options default to:
There is an example of how to set a file to parse with a ;
delimiter in example/example.php
.
::csv(mixed $file, array $options = [])
This method provides a literal parse of a file as a csv. Each line is translated to an array of values. Empty lines are not skipped.
$fileParser->csv('file.csv');
::csvColumnar(mixed $file, array $options = [])
Parses the contents of a csv as data structured columnar. Takes into account the first row of a csv file as column headers, and attaches each column header to its associated row value.
For example, a csv describing a table of contacts.
The first row is the headers of the table 'name', 'email', and 'phone'. The second row is a complete set of data. The third has a name, but empty email and phone. The fourth is not a row, it's a blank line.
$fileParser->csvColumnar('contacts.csv');
::csvRows(mixed $file, array $options = [])
Parses the contents of a csv where each row uses the first value as a key, which is set with the subsequent values. This is ideal for a csv which describes a set of key => value
pairs, or key => [ values... ]
.
$fileParser->csvRows('rows.csv');
As you can see it ignores blank lines, or lines where the key would be empty.
INI
::ini(mixed $file)
This method will parse a php ini configuration file into an array. It delegates directly to PHP's built in function parse_ini_file
.
$fileParse->ini('/etc/php55/php.ini');
JSON
::json(mixed $file)
Parses a json file into a php array. This parsing strategy delegates directly to PHP's built in json_decode
.
$fileParser->json('foobar.json');
YAML
::yaml(mixed $file)
Parses a yaml file into a php array. This parsing strategy delegates directly to symfony/Yaml
$fileParser->yaml('foobar.yml');
SplFileInfo and SplFileObject Support
Instead of passing a path to any of the file-parser methods. You can supply either a SplFileInfo
or SplFileObject
object.
Contributing
Report issues, and feel free to make requests there. Tag github issues with the best label you can. If this library doesn't do something you want, it's not difficult to extend. The library is built on the strategy pattern, new strategies can parse data differently. Write tests, and make pull requests. If you do not test your code with 100% coverage, your PR will be rejected.
Contact me
Will Vaughn
email: [email protected]
twitter: @nackjicholsonn