Skip to content

Instantly share code, notes, and snippets.

@sergiks
Created September 16, 2019 15:21
Show Gist options
  • Save sergiks/a5b07faea6f5795a7faa25cc45a9008d to your computer and use it in GitHub Desktop.
Save sergiks/a5b07faea6f5795a7faa25cc45a9008d to your computer and use it in GitHub Desktop.
/**
* Parses SMS message text from Yandex.Money
* extracts three numbers:
* confirmation code, transaction amount and account number.
*
* @param string $text message text
*
* @throws Exception on parse failure
*
* @return array
*/
function parseSMS($text) {
$result = [
'code' => '',
'amount' => '',
'account' => '',
];
$reCode = '/(\d{4,6}+)(?!,\d)/';
$reAmount = '/(\d+([,\.]\d{1,2})?)р/';
$reAccount = '/(\d{11,})/';
$match = function ($key, $re) use ($text, &$result) {
if (preg_match($re, $text, $matches)) {
$result[$key] = $matches[1];
} else {
throw new Exception( sprintf('Failed to parse %s: %s', $key, $text));
}
};
$match('code', $reCode);
$match('amount', $reAmount);
$match('account', $reAccount);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment