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.
Result of php executing
Full code of funpay_test.php
- <?php
- /**
- * @param string $message
- * @return array|null
- */
- function parse (string $message): ?array
- {
- $result = preg_match_all('/(?<number>[0-9]+)(?:(?:[,.]*(?:(?<float>[0-9]+)(?<currency>[\w ]+)| ))|$)/um', $message, $matches);
- if ($result === false || count($matches[0]) === 0) {
- return null;
- }
- $data = array_fill_keys(['password', 'wallet', 'amount'], null);
- foreach ($matches[0] as $index => $match) {
- if (strlen($matches['number'][$index]) === 14) {
- $data['wallet'] = trim($matches['number'][$index]);
- } else if (strlen($matches['currency'][$index]) > 0) {
- $data['amount'] = (float)sprintf('%s.%s', $matches['number'][$index], $matches['float'][$index]);
- } else if ($data['password'] === null) {
- $data['password'] = trim($matches['number'][$index]);
- }
- }
- return $data;
- }
- $testData = [
- [
- 'Никому не говорите пароль! Его спрашивают только мошенники.',
- 'Пароль: 68368',
- 'Перевод на счет 41001128034545',
- 'Вы потратите 7712,57р.',
- ],
- [
- 'Пароль: 4398',
- 'Спишется 502,52р.',
- 'Перевод на счет 41001128034545',
- ],
- [
- 'Кошелек: 41001128034545',
- 'Секретный код 1092',
- 'Сумма для перевода - 123.45 руб.',
- ],
- [
- 'Кошелек: 41001128034545, Секретный код 1092, Сумма для перевода - 123.45 руб.',
- ],
- [
- 'w = 41001128034545, p = 2030, a = 100.02 rub',
- ],
- [
- 'w = 41001128034545',
- 'p = 0000, a = 5,7руб.',
- ],
- ];
- foreach ($testData as $testMessageData) {
- $testMessage = implode(PHP_EOL, $testMessageData);
- echo $testMessage . PHP_EOL;
- $result = parse($testMessage);
- echo print_r($result, true) . PHP_EOL;
- }