Advertisement
Guest User

SoftUni Fancy Barcodes

a guest
Apr 6th, 2020
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.66 KB | None | 0 0
  1. <?php
  2.  
  3. $n = intval(readline());
  4. $regex = '/^(@[#]{1,})([A-Z][A-Za-z0-9]{4,}[A-Z])(@[#]{1,})/';
  5.  
  6. foreach (range(1, $n) as $i) {
  7.     $input = readline();
  8.  
  9.     echo check($input, $regex);
  10. }
  11.  
  12. function check($input, $regex)
  13. {
  14.     if (preg_match($regex, $input, $match)) {
  15.         $word = $match[2];
  16.         $productGroup = null;
  17.  
  18.         foreach (range(0, strlen($word) - 1) as $i) {
  19.             if (ctype_digit($word[$i])) {
  20.                 $productGroup .= $word[$i];
  21.             }
  22.         }
  23.  
  24.         $productGroup = $productGroup ?? '00';
  25.  
  26.         return 'Product group: ' . $productGroup . PHP_EOL;
  27.     }
  28.  
  29.     return 'Invalid barcode' . PHP_EOL;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement