Created
September 16, 2019 15:21
-
-
Save sergiks/a5b07faea6f5795a7faa25cc45a9008d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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