Skip to content

Instantly share code, notes, and snippets.

@fmtarif
Last active July 15, 2021 17:13
Show Gist options
  • Save fmtarif/c28690b90de7e3ff902612ca7c241b36 to your computer and use it in GitHub Desktop.
Save fmtarif/c28690b90de7e3ff902612ca7c241b36 to your computer and use it in GitHub Desktop.
#php Bkash - Parse number of bkash messages and get total received amount
<?php
//messages.txt sample content
/*
You have received Tk 800.00 from 0199999999.Ref XYZ. Fee Tk 0.00. Balance Tk 0.00. TrxID XYZABC at 15/07/2021 11:00
You have received deposit from iBanking of Tk 1,000.00 from Eastern Bank Limited Internet Banking. Fee Tk 0.00. Balance Tk 0.00. TrxID XYZABC at 14/07/2021 16:43
Cash In Tk 3,260.00 from 0199999999 successful. Fee Tk 0.00. Balance Tk 0.00. TrxID XYZABC at 14/07/2021 20:19. Download App: https://bKa.sh/8app
*/
//END messages.txt sample content
/**
* This script will parse the first occurence of <Tk %d> from each line and then sum those
*/
$amounts= array_map(function($text) {
$matches = [];
preg_match("/Tk ([\d\,]+)/", $text, $matches); //first occurence of /Tk <amount>/
if (!empty($matches[1])) return $matches[1];
}, $texts);
$amounts = array_filter($amounts); //get rid of empty items
//get rid of commas from the formatted amounts
$amounts_sanitized = filter_var_array($amounts, FILTER_SANITIZE_NUMBER_INT);
$total = array_reduce($amounts_sanitized, function($total, $amount) {
return $amount + $total;
});
echo sprintf('Messages %s and total amount is: %s', count($amounts), number_format($total));
pr($amounts, false);
pr($amounts_sanitized, false);
echo implode(" + ", $amounts_sanitized);
function pr($whatever, $exit = true) {
echo "<pre>";
print_r($whatever);
echo "</pre>";
if ($exit) exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment