PHP code example of artkoder / kvpparser
1. Go to this page and download the library: Download artkoder/kvpparser library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
artkoder / kvpparser example snippets
$data = KvpParser::parseFile(__DIR__ . '/../data/budget.kvp');
print_r($data);
$directory = '/path/to/your/data';
$data = KvpParser::parseRecursive($directory);
$directory = '/path/to/your/data';
$includExtension = ['kvp', 'dat'];
$data = KvpParser::parseRecursive($directory, $
$csvFile = __DIR__ . '/../data/transactions.csv';
$kdpFile = __DIR__ . '/../data/transactions.kdp';
$columnMap = [
"Date" => "Transaction Date",
"Description" => "Description 1",
"Amount" => "CAD$"
];
KvpParser::csvToKvp($csvFile, $kdpFile, $columnMap);
function formatDate($data) {
$inputDate = $data['Transaction Date'];
$inputFormat = 'm/d/Y';
$outputFormat = 'Y-m-d';
// Create a DateTime object from the input date with the specified format
$dateTime = DateTime::createFromFormat($inputFormat, $inputDate);
// Check for errors in parsing the date
if ($dateTime === false) {
return false; // Return false if the parsing fails
}
// Format the DateTime object in the desired output format
$formattedDate = $dateTime->format($outputFormat);
return $formattedDate;
}
$columnMap = [
"Date" => 'formatDate',
"Account" => "Account Number",
"Description" => function($data) { return $data['Description 1'] . " - " . $data['Description 2']; },
"Amount" => "CAD$",
"Category" => "uncategorized"];
KvpParser::csvToKvp($csvFile, $kdpFile, $columnMap);