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

  1. <?php
  2.  
  3.   function extractNumeroGold($CausaleABI, $descrHB, $ABIBanca)
  4.     {
  5.         $causale = '';
  6.         $documento = '';
  7.         $dataDocumento = '';
  8.         $tipoRegola = '';
  9.         $descrHB = strtoupper($descrHB);
  10.  
  11.         function togliSpeciali($docStr)
  12.         {
  13.             $result = '';
  14.             for ($j = 0; $j < strlen($docStr); $j++) {
  15.                 if ($docStr[$j] !== '-' && $docStr[$j] !== '/') {
  16.                     $result .= $docStr[$j];
  17.                 }
  18.             }
  19.             return $result;
  20.         }
  21.  
  22.         if (trim($CausaleABI) == 48 || trim($CausaleABI) === 'ZI') {
  23.  
  24.  
  25.             if (strpos($descrHB, 'Causale:') !== false) {
  26.                 $causale = trim(substr($descrHB, strpos($descrHB, 'Causale:') + 8));
  27.                 $causale = trim($causale);
  28.             }
  29.  
  30.             if (trim($ABIBanca) == '01030') {
  31.                 if (strpos($descrHB, 'INF:EE:') !== false) {
  32.                     $causale = trim(substr($descrHB, strpos($descrHB, 'INF:EE:') + 7));
  33.                     $causale = trim($causale);
  34.                 }
  35.  
  36.                 if (strpos($descrHB, 'INF:RI:') !== false) {
  37.                     $causale = trim(substr($descrHB, strpos($descrHB, 'INF:RI:') + 7));
  38.                     $causale = trim($causale);
  39.                 }
  40.             }
  41.  
  42.             if (trim($ABIBanca) == '03069') {
  43.                 $causale = trim(substr($descrHB, strpos($descrHB, 'CON CONTAB.') + 11, strpos($descrHB, 'Bonifico a Vostro favore') - (strpos($descrHB, 'CON CONTAB.') + 11)));
  44.                 $causale = trim($causale);
  45.             }
  46.  
  47.             if (trim($ABIBanca) == '02008') {
  48.                 if (strpos($descrHB, ' COMM ') !== false) {
  49.                     $causale = trim(substr($descrHB, strpos($descrHB, ' PER ') + 5, strpos($descrHB, ' COMM ') - (strpos($descrHB, ' PER ') + 5)));
  50.                 } else {
  51.                     $causale = trim(substr($descrHB, strpos($descrHB, ' PER ') + 5, strpos($descrHB, ' TRN ') - (strpos($descrHB, ' PER ') + 5)));
  52.                 }
  53.                 $causale = trim($causale);
  54.             }
  55.  
  56.             if (trim($ABIBanca) == '03441' || trim($ABIBanca) == '05385' || trim($ABIBanca) == '05484') {
  57.                 $causale = trim(substr($descrHB, strpos($descrHB, 'Note:') + 5));
  58.                 $causale = trim($causale);
  59.             }
  60.  
  61.             if (trim($ABIBanca) == '05034') {
  62.                 $causale = trim(substr($descrHB, strpos($descrHB, '- bon.da') + 8));
  63.                 $causale = trim($causale);
  64.             }
  65.  
  66.             if (trim($ABIBanca) == '05336') {
  67.                 $causale = trim(substr($descrHB, strpos($descrHB, 'SCT:') + 4));
  68.                 $causale = trim($causale);
  69.             }
  70.  
  71.             if (trim($ABIBanca) == '05387') {
  72.                 $causale = trim(substr($descrHB, strpos($descrHB, 'a favore di') + 11));
  73.                 $causale = trim($causale);
  74.             }
  75.  
  76.             if (trim($causale) == '') {
  77.                 $causale = $descrHB;
  78.             }
  79.  
  80.             // se non ha trovato causale
  81.             if (trim($causale) !== '') {
  82.                 $trovato = false;
  83.  
  84.                 if (strpos(strtoupper($causale), 'FATTURA') !== false && !$trovato) {
  85.                     $trovato = true;
  86.                     $tipoRegola = 'FATTURA';
  87.                     $z = strpos(strtoupper($causale), 'FATTURA') + 8;
  88.                 }
  89.  
  90.                 if (strpos(strtoupper($causale), 'FATT') !== false && !$trovato) {
  91.                     $trovato = true;
  92.                     $tipoRegola = 'FATT';
  93.                     $z = strpos(strtoupper($causale), 'FATT') + 5;
  94.                 }
  95.  
  96.                 if (strpos(strtoupper($causale), 'SALDO FT') !== false && !$trovato) {
  97.                     $trovato = true;
  98.                     $tipoRegola = 'SALDO FT';
  99.                     $z = strpos(strtoupper($causale), 'SALDO FT') + 9;
  100.                 }
  101.  
  102.                 if (strpos(strtoupper($causale), 'SALDO FATTURA') !== false && !$trovato) {
  103.                     $trovato = true;
  104.                     $tipoRegola = 'SALDO FATTURA';
  105.                     $z = strpos(strtoupper($causale), 'SALDO FATTURA') + 9;
  106.                 }
  107.  
  108.                 if (strpos(strtoupper($causale), 'S.DO FT') !== false && !$trovato) {
  109.                     $trovato = true;
  110.                     $tipoRegola = 'S.DO FT';
  111.                     $z = strpos(strtoupper($causale), 'S.DO FT') + 7;
  112.                 }
  113.  
  114.                 if (strpos(strtoupper($causale), 'PAG.FT') !== false && !$trovato) {
  115.                     $trovato = true;
  116.                     $tipoRegola = 'PAG.FT';
  117.                     $z = strpos(strtoupper($causale), 'PAG.FT') + 6;
  118.                 }
  119.  
  120.                 if (strpos(strtoupper($causale), 'PAG. FT') !== false && !$trovato) {
  121.                     $trovato = true;
  122.                     $tipoRegola = 'PAG. FT';
  123.                     $z = strpos(strtoupper($causale), 'PAG. FT') + 7;
  124.                 }
  125.  
  126.                 if (strpos(strtoupper($causale), 'SDO FT') !== false && !$trovato) {
  127.                     $trovato = true;
  128.                     $tipoRegola = 'SDO FT';
  129.                     $z = strpos(strtoupper($causale), 'SDO FT') + 6;
  130.                 }
  131.                 if (strpos(strtoupper($causale), 'SALDO') !== false && !$trovato) {
  132.                     $trovato = true;
  133.                     $tipoRegola = 'SALDO';
  134.                     $z = strpos(strtoupper($causale), 'SALDO') + 5;
  135.                 }
  136.  
  137.                 if (strpos(strtoupper($causale), 'INV/') !== false && !$trovato) {
  138.                     $trovato = true;
  139.                     $tipoRegola = 'INV/';
  140.                     $z = strpos(strtoupper($causale), 'INV/') + 4;
  141.                 }
  142.  
  143.                 if ($trovato) {
  144.                     $inizio = false;
  145.                     $fine = false;
  146.  
  147.                     while ($z <= strlen($causale) && !$fine) {
  148.                         if (!$inizio && ($causale[$z] === '0' || $causale[$z] === '1' || $causale[$z] === '2' || $causale[$z] === '3' || $causale[$z] === '4' || $causale[$z] === '5' || $causale[$z] === '6' || $causale[$z] === '7' || $causale[$z] === '8' || $causale[$z] === '9' || $causale[$z] === '/' || $causale[$z] === '-')) {
  149.                             $inizio = true;
  150.                         }
  151.  
  152.                         if ($inizio) {
  153.                             if ($causale[$z] === '0' || $causale[$z] === '1' || $causale[$z] === '2' || $causale[$z] === '3' || $causale[$z] === '4' || $causale[$z] === '5' || $causale[$z] === '6' || $causale[$z] === '7' || $causale[$z] === '8' || $causale[$z] === '9' || $causale[$z] === '/' || $causale[$z] === '-') {
  154.                                 $documento .= $causale[$z];
  155.                             } else {
  156.                                 try {
  157.                                     $annoDocumento = (int)trim(togliSpeciali(trim($documento)));
  158.  
  159.                                     if ($annoDocumento >= 2018 && $annoDocumento <= date('Y')) {
  160.                                         $documento = '';
  161.                                         $inizio = false;
  162.                                     } else {
  163.                                         $fine = true;
  164.                                     }
  165.                                 } catch (Exception $e) {
  166.                                     $fine = true;
  167.                                 }
  168.                             }
  169.                         }
  170.                         $z += 1;
  171.                     }
  172.                 }
  173.  
  174.                 $y = strlen($causale);
  175.  
  176.  
  177.                 // cerco Data a 10 Caratteri
  178.                 while ($y >= 10 && !$trovato) {
  179.                     $dataDocumento = substr($causale, $y - 9, 10);
  180.                     if (in_array($dataDocumento[2], array('/', '.', '-', ' ')) && $dataDocumento[5] === $dataDocumento[2]) {
  181.                         try {
  182.                             $giorno = (int)substr($dataDocumento, 0, 2);
  183.                             $mese = (int)substr($dataDocumento, 3, 2);
  184.                             $anno = (int)substr($dataDocumento, 6, 4);
  185.  
  186.                             if (checkdate($mese, $giorno, $anno) && date('Y') >= $anno) {
  187.                                 $trovato = true;
  188.                             }
  189.                         } catch (Exception $e) {
  190.                             // dataDocumento := '';
  191.                         }
  192.                     }
  193.  
  194.                     if (!$trovato && in_array($dataDocumento[4], array('/', '.', '-', ' ')) && $dataDocumento[7] === $dataDocumento[4]) {
  195.                         try {
  196.                             $anno = (int)substr($dataDocumento, 0, 4);
  197.                             $mese = (int)substr($dataDocumento, 5, 2);
  198.                             $giorno = (int)substr($dataDocumento, 8, 2);
  199.  
  200.                             if (checkdate($mese, $giorno, $anno) && date('Y') >= $anno) {
  201.                                 $trovato = true;
  202.                             }
  203.                         } catch (Exception $e) {
  204.                             // dataDocumento := '';
  205.                         }
  206.                     }
  207.  
  208.                     $documento = '';
  209.                     $z = $y - 10;
  210.  
  211.                     if ($trovato) {
  212.                         $tipoRegola = 'Data a 10 Caratteri';
  213.                         $inizio = false;
  214.                         $fine = false;
  215.  
  216.                         while ($z > 1 && !$fine) {
  217.                             if (!$inizio && in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
  218.                                 $inizio = true;
  219.                             }
  220.  
  221.                             if ($inizio) {
  222.                                 if ($inizio) {
  223.                                     if (in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
  224.                                         $documento = $causale[$z] . $documento;
  225.                                     } else {
  226.                                         try {
  227.                                             $annoDocumento = (int)togliSpeciali(trim(togliSpeciali($documento)));
  228.  
  229.                                             if ($annoDocumento >= 2018 && $annoDocumento <= date('Y')) {
  230.                                                 $documento = '';
  231.                                                 $inizio = false;
  232.                                             } else {
  233.                                                 $fine = true;
  234.                                             }
  235.                                         } catch (Exception $e) {
  236.                                             $fine = true;
  237.                                         }
  238.                                     }
  239.                                 }
  240.                             }
  241.  
  242.                             $z -= 1;
  243.                         }
  244.                     } else {
  245.                         $dataDocumento = '';
  246.                     }
  247.  
  248.                     $y -= 1;
  249.                 }
  250.  
  251.  
  252.                 // Data a 8 caratteri
  253.                 if (!$trovato) {
  254.                     $y = strlen($causale);
  255.                     while ($y >= 8 && !$trovato) {
  256.                         $dataDocumento = substr($causale, $y - 7, 8);
  257.                         if (in_array($dataDocumento[2], array('/', '.', '-', ' ')) && $dataDocumento[5] === $dataDocumento[2]) {
  258.                             try {
  259.                                 $giorno = (int)substr($dataDocumento, 0, 2);
  260.                                 $mese = (int)substr($dataDocumento, 3, 2);
  261.                                 $anno = 2000 + (int)substr($dataDocumento, 6, 2);
  262.  
  263.                                 if (checkdate($mese, $giorno, $anno) && date('Y') >= $anno) {
  264.                                     $trovato = true;
  265.                                 }
  266.                             } catch (Exception $e) {
  267.                             }
  268.                         }
  269.  
  270.                         if (!$trovato && in_array($dataDocumento[2], array('/', '.', '-', ' ')) && $dataDocumento[5] === $dataDocumento[2]) {
  271.                             try {
  272.                                 $anno = 2000 + (int)substr($dataDocumento, 0, 2);
  273.                                 $mese = (int)substr($dataDocumento, 3, 2);
  274.                                 $giorno = (int)substr($dataDocumento, 6, 2);
  275.  
  276.                                 if (checkdate($mese, $giorno, $anno) && date('Y') >= $anno) {
  277.                                     $trovato = true;
  278.                                 }
  279.                             } catch (Exception $e) {
  280.                             }
  281.                         }
  282.  
  283.                         $documento = '';
  284.                         $z = $y - 8;
  285.  
  286.                         if ($trovato) {
  287.                             $tipoRegola = 'Data a 8 Caratteri';
  288.                             $inizio = false;
  289.                             $fine = false;
  290.  
  291.                             while ($z > 1 && !$fine) {
  292.                                 if (!$inizio && in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
  293.                                     $inizio = true;
  294.                                 }
  295.  
  296.                                 if ($inizio) {
  297.                                     if ($inizio) {
  298.                                         if (in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
  299.                                             $documento = $causale[$z] . $documento;
  300.                                         } else {
  301.                                             try {
  302.                                                 $annoDocumento = (int)togliSpeciali(trim($documento));
  303.  
  304.                                                 if ($annoDocumento >= 2018 && $annoDocumento <= date('Y')) {
  305.                                                     $documento = '';
  306.                                                     $inizio = false;
  307.                                                 } else {
  308.                                                     $fine = true;
  309.                                                 }
  310.                                             } catch (Exception $e) {
  311.                                                 $fine = true;
  312.                                             }
  313.                                         }
  314.                                     }
  315.                                 }
  316.  
  317.                                 $z -= 1;
  318.                             }
  319.                         } else {
  320.                             $dataDocumento = '';
  321.                         }
  322.  
  323.                         $y -= 1;
  324.                     }
  325.  
  326.                     // Prendo gli ultimi caratteri per il numero
  327.                     if (!$trovato) {
  328.                         $tipoRegola = 'Documento';
  329.                         $z = strlen($causale);
  330.                         $inizio = false;
  331.                         $fine = false;
  332.  
  333.                         while ($z >= 1 && !$fine) {
  334.                             if (!$inizio && isset($causale[$z]) && ($causale[$z] === '0' || $causale[$z] === '1' || $causale[$z] === '2' || $causale[$z] === '3' || $causale[$z] === '4' || $causale[$z] === '5' || $causale[$z] === '6' || $causale[$z] === '7' || $causale[$z] === '8' || $causale[$z] === '9' || $causale[$z] === '/' || $causale[$z] === '-')) {
  335.                                 $inizio = true;
  336.                             }
  337.  
  338.                             if ($inizio) {
  339.                                 if ($inizio) {
  340.                                     if (in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
  341.                                         $documento = $causale[$z] . $documento;
  342.                                     } else {
  343.                                         try {
  344.                                             $annoDocumento = (int)togliSpeciali(trim($documento));
  345.  
  346.                                             if ($annoDocumento >= 2018 && $annoDocumento <= date('Y')) {
  347.                                                 $documento = '';
  348.                                                 $inizio = false;
  349.                                             } else {
  350.                                                 $fine = true;
  351.                                             }
  352.                                         } catch (Exception $e) {
  353.                                             $fine = true;
  354.                                         }
  355.                                     }
  356.                                 }
  357.                             }
  358.  
  359.                             $z -= 1;
  360.                         }
  361.                     }
  362.                 }
  363.  
  364.  
  365.  
  366.  
  367.             }
  368.         }
  369.  
  370.         if (!empty($documento)) {
  371.             //pulisco inizio e fine
  372.             $primoCarattere = $documento[0];
  373.             $ultimoCarattere = $documento[strlen($documento) - 1];
  374.  
  375.             if ($primoCarattere === '-' || $primoCarattere === '/') {
  376.                 $documento = substr($documento, 1);
  377.             }
  378.  
  379.             if ($ultimoCarattere === '-' || $ultimoCarattere === '/') {
  380.                 $documento = substr($documento, 0, -1);
  381.             }
  382.         }
  383.  
  384.         $result = [];
  385.         $result['causale'] = $causale;
  386.         $result['numero_documento'] = $documento;
  387.         $result['data_documento'] = $dataDocumento;
  388.         $result['regola'] = $tipoRegola;
  389.         return $result;
  390.     }
  391.     
  392. $CausaleABI = '48';
  393. $ABIBanca = '';
  394.  
  395. $descrHB = "/NC/012104120CXZ51/BA/03069/BC/12312/OB/DOBERMANN EUROPE SRL/BB/MUST WEB SRL/MP/SALDO FATT. 202021/RO/AH003";
  396.  
  397.  
  398. $result = extractNumeroGold($CausaleABI,$descrHB,$ABIBanca);
  399. echo 'causale: ' .$result['causale'];
  400. echo "\n";
  401. echo 'regola: ' .$result['regola'];
  402. echo "\n";
  403. echo 'data_documento: ' .$result['data_documento'];
  404. echo "\n";
  405. echo 'numero_documento: ' .$result['numero_documento'];
  406.     
File Description
  • extractNumeroGold
  • PHP Code
  • 15 May-2023
  • 17.38 Kb
You can Share it: