Skip to content

Instantly share code, notes, and snippets.

@dictcp
Last active January 11, 2022 06:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dictcp/cd9e3028b9b873663ff0 to your computer and use it in GitHub Desktop.
Save dictcp/cd9e3028b9b873663ff0 to your computer and use it in GitHub Desktop.
Convert Hang Seng Bank eStatement To CSV file
<?php
// working command
// pdftotext -layout eStatementFile-2014-01-30.pdf - | grep -ozP 'TRANSACTION HISTORY(.|\n)*?\n\n\n' | php ~/a.php | fgrep '.' | grep -v "B/F BALANCE" | sed 's/,//g' | sed 's/;/,/g'
function trim_value(&$value)
{
$value=str_replace(',', '', $value);
$value = trim($value);
}
$fields=[];
$fp = fopen('php://stdin','r') or die ("can't open file");
while ($s = fgets($fp,1024)) {
$s=str_pad($s,142);
$target = unpack('A11date/A58title/A27deposit/A24withdraw/A21balance',$s);
$last_i = count($fields)-1;
if ($last_i>1 && empty($target['date'])) {
$target['date'] = $fields[$last_i]['date'];
}
if (
$last_i>1 &&
(empty($fields[$last_i]['deposit']) && empty($fields[$last_i]['withdraw']) && empty($fields[$last_i]['balance'])) &&
(empty($target['deposit']) || empty($target['withdraw']) || empty($target['balance']))
) {
foreach ($fields[$last_i] as $k => &$v) {
if($k=='date')continue;
$v.=$target[$k];
}
array_walk($fields[$last_i], 'trim_value');
} else {
array_walk($target, 'trim_value');
$fields[]=$target;
}
}
array_map(function(array $x){
$x['date']=$x['date'] . " 2014";
$x['date']=strftime('%Y-%m-%d',strtotime($x['date']));
$x['amount']=$x['deposit']-$x['withdraw'];
unset($x['deposit']);
unset($x['withdraw']);
unset($x['balance']);
echo implode(';',$x)."\n";
}
,$fields);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment