3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); static $text1 = ' Пароль: 0936 Спишется 1241р. Перевод на счет 410012124208089 '; static $text2 = ' Пароль: 3042 Спишется 1240,21р. Перевод на счет 410012124208089 '; static $text3 = ' Пароль: 3042 Спишется 1240,21 руб Перевод на счет 410012124208089 '; function parse(string $text): array { $count = preg_match_all(/** @lang RegExp */ '/ (?: # самое простое — кошелёк получателя. Считаем, что он 15 символов (?<address>\d{15}) # дальше самое сложное — сумма перевода. Может быть целым или с разделителем копеек, в этом и сложность # так как может быть целым, то нам нужно её отличить от кода подтверждения |(?: (?<sum> \d+ (?:[,.]{1}\d+|) ) # будем считать, что после суммы будет написана валюта. Иначе не угадать что — код, а что — сумма. # допустим, что может быть слитно или через пробелы # допустим, что по-английски или по-русски, с маленькой или с большой буквы (?i:\s*[рr]) ) # дальше опять просто — код подтверждения. Просто целое число |(?<code>\d+) ) /x', $text, $matches, PREG_PATTERN_ORDER); var_dump($matches); die; if ($count !== 3) { throw new RuntimeException('Всё пропало, нужно писать новую регулярку'); } $sum = 0; $code = 0; $address = 0; for ($i = 0; $i < 3; $i++) { [$tempSum, $tempCode, $tempAddress] = [$matches['sum'][$i], $matches['code'][$i], $matches['address'][$i]]; if ($tempSum) { $sum = $tempSum; } if ($tempCode) { $code = $tempCode; } if ($tempAddress) { $address = $tempAddress; } } return compact('code', 'sum', 'address'); } echo $text1 . PHP_EOL; $result = parse($text1); var_dump($result); echo $text2 . PHP_EOL; $result = parse($text2); var_dump($result); echo $text3 . PHP_EOL; $result = parse($text3); var_dump($result);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Пароль: 0936 Спишется 1241р. Перевод на счет 410012124208089 array(7) { [0]=> array(3) { [0]=> string(4) "0936" [1]=> string(5) "1241�" [2]=> string(15) "410012124208089" } ["address"]=> array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(15) "410012124208089" } [1]=> array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(15) "410012124208089" } ["sum"]=> array(3) { [0]=> string(0) "" [1]=> string(4) "1241" [2]=> string(0) "" } [2]=> array(3) { [0]=> string(0) "" [1]=> string(4) "1241" [2]=> string(0) "" } ["code"]=> array(3) { [0]=> string(4) "0936" [1]=> string(0) "" [2]=> string(0) "" } [3]=> array(3) { [0]=> string(4) "0936" [1]=> string(0) "" [2]=> string(0) "" } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Пароль: 0936 Спишется 1241р. Перевод на счет 410012124208089 array(7) { [0]=> array(3) { [0]=> string(4) "0936" [1]=> string(5) "1241�" [2]=> string(15) "410012124208089" } ["address"]=> array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(15) "410012124208089" } [1]=> array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(15) "410012124208089" } ["sum"]=> array(3) { [0]=> string(0) "" [1]=> string(4) "1241" [2]=> string(0) "" } [2]=> array(3) { [0]=> string(0) "" [1]=> string(4) "1241" [2]=> string(0) "" } ["code"]=> array(3) { [0]=> string(4) "0936" [1]=> string(0) "" [2]=> string(0) "" } [3]=> array(3) { [0]=> string(4) "0936" [1]=> string(0) "" [2]=> string(0) "" } }
Output for 7.0.0 - 7.0.33
Parse error: syntax error, unexpected '=' in /in/tOEoM on line 60
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Warning: Unsupported declare 'strict_types' in /in/tOEoM on line 3 Parse error: syntax error, unexpected ':', expecting '{' in /in/tOEoM on line 23
Process exited with code 255.

preferences:
247.09 ms | 401 KiB | 287 Q