Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1.     public function parseSms(string $sms): array
  2.     {
  3.         $accountPattern = '(41001\d{8,11})';
  4.         $sumPattern = '(\d+(?:[,.]\d+)?)р';
  5.         $codePattern = '(\d+)';
  6.  
  7.         $countMatches = preg_match_all("/$accountPattern|$sumPattern|$codePattern/", $sms, $matches, PREG_SET_ORDER);
  8.  
  9.         if ($countMatches === false)
  10.             throw new Error('Ошибка при попытке разбора СМС', preg_last_error());
  11.         elseif ($countMatches === 0)
  12.             throw new Error('Не удалось получить никаких данных из СМС');
  13.  
  14.         $code = $sum = $account = null;
  15.  
  16.         foreach ($matches as $match) {
  17.             if ($match[1])
  18.                 $account = $match[1];
  19.             elseif ($match[2])
  20.                 $sum = $match[2];
  21.             elseif ($match[3])
  22.                 $code = $match[3];
  23.         }
  24.  
  25.         return compact('code', 'sum', 'account');
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement