Download the PHP package artkoder/kvpparser without Composer
On this page you can find all versions of the php package artkoder/kvpparser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download artkoder/kvpparser
More information about artkoder/kvpparser
Files in artkoder/kvpparser
Package kvpparser
Short Description A library to read and parse files with key value pairs.
License MIT
Informations about the package kvpparser
KVP Parser
A library to read and parse files with key value pairs.
What is a KVP file
A KVP file consists of records of key value pairs separated by empty lines.
Here is an example:
Here is a differnt example with a comment. Lines starting with #
are comments ignored by the parser.
I started using this type of file in different projects because it's similar to yaml but without the nesting and it's parsing complexity. So I decided to build a library to make the code reusable.
Installation
Using composer
How to use this library
Here are some examples:
Parse a single file
Supose we have the following file named budget.kvp
:
We can parse the file and receive an associative array by running the following code:
Here is the output:
Parse multiple files inside a directory recursively
You can specify a directory. KvpParser will parse all the files found in that directory and subdirectories.
By default KvpParser will look for files with extension .kvp
. But you can specify
any file extensions. For example:
Convert a CSV file to a KVP file
This is very convenient. Suppose that you have a csv file with the following data from your bank statement:
Let's say that we are only interested in the following data: Date, Description 1 and CAD$.
But we also want to change the column names in our resultin .kdp
file.
Here is what we can do:
Here are the contents of transactions.kdp
:
If no $columnMap
is specified, the column names in the csv
file will
become the name of the properties in the kvp
file.
Some times you need to do some processing of data during mapping, we can do that adding closures to the map array.
In our previous example, suppose you want ot combine both description columns
and format date to be Y-m-d
.
Here is how you would do it:
Notice also that the column uncategorized
does not exist. In this case
the word uncategorized
would be use as the value.