funpay_test - 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 funpay_test.php

  1. <?php
  2.  
  3. /**
  4.  * @param string $message
  5.  * @return array|null
  6.  */
  7. function parse (string $message): ?array
  8. {
  9.     $result = preg_match_all('/(?<number>[0-9]+)(?:(?:[,.]*(?:(?<float>[0-9]+)(?<currency>[\w ]+)| ))|$)/um', $message, $matches);
  10.  
  11.     if ($result === false || count($matches[0]) === 0) {
  12.         return null;
  13.     }
  14.  
  15.     $data = array_fill_keys(['password', 'wallet', 'amount'], null);
  16.  
  17.     foreach ($matches[0] as $index => $match) {
  18.         if (strlen($matches['number'][$index]) === 14) {
  19.             $data['wallet'] = trim($matches['number'][$index]);
  20.         } else if (strlen($matches['currency'][$index]) > 0) {
  21.             $data['amount'] = (float)sprintf('%s.%s', $matches['number'][$index], $matches['float'][$index]);
  22.         } else if ($data['password'] === null) {
  23.             $data['password'] = trim($matches['number'][$index]);
  24.         }
  25.     }
  26.  
  27.     return $data;
  28. }
  29.  
  30. $testData = [
  31.     [
  32.         'Никому не говорите пароль! Его спрашивают только мошенники.',
  33.         'Пароль: 68368',
  34.         'Перевод на счет 41001128034545',
  35.         'Вы потратите 7712,57р.',
  36.     ],
  37.     [
  38.         'Пароль: 4398',
  39.         'Спишется 502,52р.',
  40.         'Перевод на счет 41001128034545',
  41.     ],
  42.     [
  43.         'Кошелек: 41001128034545',
  44.         'Секретный код 1092',
  45.         'Сумма для перевода - 123.45 руб.',
  46.     ],
  47.     [
  48.         'Кошелек: 41001128034545, Секретный код 1092, Сумма для перевода - 123.45 руб.',
  49.     ],
  50.     [
  51.         'w = 41001128034545, p = 2030, a = 100.02 rub',
  52.     ],
  53.     [
  54.         'w = 41001128034545',
  55.         'p = 0000, a = 5,7руб.',
  56.     ],
  57. ];
  58.  
  59. foreach ($testData as $testMessageData) {
  60.     $testMessage = implode(PHP_EOL, $testMessageData);
  61.     echo $testMessage . PHP_EOL;
  62.     $result = parse($testMessage);
  63.     echo print_r($result, true) . PHP_EOL;
  64. }
  65.  
File Description
  • funpay_test
  • PHP Code
  • 21 Apr-2019
  • 1.92 Kb
You can Share it: