Skip to content

Instantly share code, notes, and snippets.

@SmithWebster
Created October 4, 2016 22:52
Show Gist options
  • Save SmithWebster/9c632eea3db3c8c849df95dc78f5a938 to your computer and use it in GitHub Desktop.
Save SmithWebster/9c632eea3db3c8c849df95dc78f5a938 to your computer and use it in GitHub Desktop.
<?php
function parseSms($content) {
$result = [
'code' => null,
'sum' => null,
'account' => null,
];
$code = null;
$sum = null;
$account = null;
$codeRegexp = "~[^\d]+:\s*(\d{3,})~";
$sumRegexp = "~[^\d]+(\d+(\.|,)\d{2})\s*(р|руб|рубл(я|ей))~";
$accountRegexp = "~[^\d]+(\d{10,16})[^\d]*~";
$matches = [];
preg_match($codeRegexp, $content, $matches);
$result['code'] = @$matches[1] ?: null;
preg_match($sumRegexp, $content, $matches);
$result['sum'] = @$matches[1] ?: null;
preg_match($accountRegexp, $content, $matches);
$result['account'] = @$matches[1] ?: null;
return $result;
}
function smsGenerator() {
$sms = <<<EOF
Пароль: 0308
Спишется 395,97р.
Перевод на счет 41001000000000
EOF;
yield $sms;
$sms = <<<EOF
Password:03082526
Будет снято: 395.97 рублей.
Счет получателя - 41001075780093. Бла-бла, какой-то еще текст.
EOF;
yield $sms;
}
$isCli = php_sapi_name() == 'cli';
if (!$isCli) {
echo("<pre>");
}
foreach (smsGenerator() as $sms) {
$res = parseSms($sms);
echo "=== SMS for parsing:\n";
echo $sms . "\n";
echo "=== Parsed data from SMS:\n";
echo json_encode($res) . "\n";
echo "\n";
}
if (!$isCli) {
echo("</pre>");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<style type="text/css" media="all">
body {
margin: 10px;
}
</style>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"
type="text/javascript"
charset="utf-8"
></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
$('#myModal').modal();
function makeQuery(url, contentElement) {
contentElement.html('Please wait! Loading...');
$.ajax({
url: url,
success: function(result) {
contentElement.html(result)
},
error: function(error) {
var errorMessage = null;
if (error.state() == 'rejected') {
errorMessage = 'XMLHttpRequest cannot load '
+ url + ". You should check the CORS.";
contentElement.html(errorMessage);
} else {
errorMessage = error;
}
contentElement.html(errorMessage);
}
});
}
</script>
</head>
<body>
<div>
<input
type="url"
name="url"
id="url"
value="/first.php"
class="form-control"
placeholder="Please enter request URL"
style="width: 400px;"
/>
<button type="button" class="btn btn-primary btn-lg"
data-toggle="modal" data-target="#myModal"
onclick="makeQuery($('#url').val(), $('#modalBody'))"
>
Send Request
</button>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1"
role="dialog"
aria-labelledby="myModalLabel"
>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="myModalLabel">Response</h4>
</div>
<div class="modal-body" id="modalBody"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">Close</button>
<button type="button"
class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment