SMS Parser - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of SMS Parser.php

  1. <?php
  2.  
  3. $strings = [
  4.     "Пароль: 8191\nСпишется 1329,87р.\nПеревод на счет 4100175017397\n",
  5.     "Password: 1234\nAmount 1329,87р.\nWallet 4100175017397\n",
  6.     "Вы переведете 1329,87р на кошелек 4100175017397 для подтверждения введите пароль 0928\n",
  7.     "На кошелек 4100175017397 будет переведано 1329,87 рублей для подтверждения введите пароль 7711\n",
  8.     "Пароль: 6205Спишется 1240,21р.Перевод на счет 4100175017397",
  9.     "Not enough funds.",
  10. ];
  11.  
  12. foreach ($strings as $string) {
  13.     $paymentInfo = parseSMS($string);
  14.     echo "Входящие данные:\n--------------------\n".$string."--------------------";
  15.     echo "\n\tРезультат:";
  16.     if ($paymentInfo !== null) {
  17.         echo "\n\tКошелек: ".$paymentInfo['wallet']."\n\tСумма: ".$paymentInfo['amount']."\n\tPIN: ".$paymentInfo['pin'];
  18.         // save function
  19.     } else {
  20.         echo "\n\t Не удалось спарсить сообщение";
  21.     }
  22.     echo "\n\n";
  23. }
  24.  
  25. function parseSMS($text) {
  26.     preg_match("/(\d+[\,]\d{2})/", $text, $amount_regex);
  27.     preg_match("/(\d{4})/", $text, $pin_regex);
  28.     preg_match("/(\d{13})/", $text, $wallet_regex);
  29.     if (isset($amount_regex['1']) && isset($pin_regex['1']) && isset($wallet_regex['1']) ) {
  30.         return [
  31.             'amount' => $amount_regex['1'],
  32.             'pin' => $pin_regex['1'],
  33.             'wallet' => $wallet_regex['1']
  34.         ];
  35.     }
  36.     return null;
  37. }
File Description
  • SMS Parser
  • PHP Code
  • 19 Feb-2020
  • 1.55 Kb
You can Share it: