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