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.
Result of php executing
Full code of extractNumeroGold.php
- <?php
- function extractNumeroGold($CausaleABI, $descrHB, $ABIBanca)
- {
- /*$y;
- $z;
- $causale;
- $dataDocumento;
- $documento;
- $tipoRegola;
- $anno;
- $mese;
- $giorno;
- $trovato;
- $inizio;
- $fine;
- $annoDocumento;
- $updDB;*/
- function togliSpeciali($docStr)
- {
- $result = '';
- for ($j = 0; $j < strlen($docStr); $j++) {
- if ($docStr[$j] !== '-' && $docStr[$j] !== '/') {
- $result .= $docStr[$j];
- }
- }
- return $result;
- }
- if (trim($CausaleABI) === '48' || trim($CausaleABI) === 'ZI') {
- $causale = '';
- $documento = '';
- $dataDocumento = '';
- $tipoRegola = '';
- if (strpos($descrHB, 'Causale:') !== false) {
- $causale = trim(substr($descrHB, strpos($descrHB, 'Causale:') + 8));
- $causale = trim($causale);
- }
- if (trim($ABIBanca) === '01030') {
- if (strpos($descrHB, 'INF:EE:') !== false) {
- $causale = trim(substr($descrHB, strpos($descrHB, 'INF:EE:') + 7));
- $causale = trim($causale);
- }
- if (strpos($descrHB, 'INF:RI:') !== false) {
- $causale = trim(substr($descrHB, strpos($descrHB, 'INF:RI:') + 7));
- $causale = trim($causale);
- }
- }
- if (trim($ABIBanca) === '03069') {
- $causale = trim(substr($descrHB, strpos($descrHB, 'CON CONTAB.') + 11, strpos($descrHB, 'Bonifico a Vostro favore') - (strpos($descrHB, 'CON CONTAB.') + 11)));
- $causale = trim($causale);
- }
- if (trim($ABIBanca) === '02008') {
- if (strpos($descrHB, ' COMM ') !== false) {
- $causale = trim(substr($descrHB, strpos($descrHB, ' PER ') + 5, strpos($descrHB, ' COMM ') - (strpos($descrHB, ' PER ') + 5)));
- } else {
- $causale = trim(substr($descrHB, strpos($descrHB, ' PER ') + 5, strpos($descrHB, ' TRN ') - (strpos($descrHB, ' PER ') + 5)));
- }
- $causale = trim($causale);
- }
- if (trim($ABIBanca) === '03441' || trim($ABIBanca) === '05385' || trim($ABIBanca) === '05484') {
- $causale = trim(substr($descrHB, strpos($descrHB, 'Note:') + 5));
- $causale = trim($causale);
- }
- if (trim($ABIBanca) === '05034') {
- $causale = trim(substr($descrHB, strpos($descrHB, '- bon.da') + 8));
- $causale = trim($causale);
- }
- if (trim($ABIBanca) === '05336') {
- $causale = trim(substr($descrHB, strpos($descrHB, 'SCT:') + 4));
- $causale = trim($causale);
- }
- if (trim($ABIBanca) === '05387') {
- $causale = trim(substr($descrHB, strpos($descrHB, 'a favore di') + 11));
- $causale = trim($causale);
- }
- if (trim($causale) === '') {
- $causale = $descrHB;
- }
- // se non ha trovato causale
- if (trim($causale) !== '') {
- $trovato = false;
- if (strpos(strtoupper($causale), 'FATTURA') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'FATTURA';
- $z = strpos(strtoupper($causale), 'FATTURA') + 8;
- }
- if (strpos(strtoupper($causale), 'FATT') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'FATT';
- $z = strpos(strtoupper($causale), 'FATT') + 5;
- }
- if (strpos(strtoupper($causale), 'SALDO FT') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'SALDO FT';
- $z = strpos(strtoupper($causale), 'SALDO FT') + 9;
- }
- if (strpos(strtoupper($causale), 'SALDO FATTURA') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'SALDO FATTURA';
- $z = strpos(strtoupper($causale), 'SALDO FATTURA') + 9;
- }
- if (strpos(strtoupper($causale), 'S.DO FT') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'S.DO FT';
- $z = strpos(strtoupper($causale), 'S.DO FT') + 7;
- }
- if (strpos(strtoupper($causale), 'PAG.FT') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'PAG.FT';
- $z = strpos(strtoupper($causale), 'PAG.FT') + 6;
- }
- if (strpos(strtoupper($causale), 'PAG. FT') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'PAG. FT';
- $z = strpos(strtoupper($causale), 'PAG. FT') + 7;
- }
- if (strpos(strtoupper($causale), 'SDO FT') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'SDO FT';
- $z = strpos(strtoupper($causale), 'SDO FT') + 6;
- }
- if (strpos(strtoupper($causale), 'SALDO') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'SALDO';
- $z = strpos(strtoupper($causale), 'SALDO') + 5;
- }
- if (strpos(strtoupper($causale), 'INV/') !== false && !$trovato) {
- $trovato = true;
- $tipoRegola = 'INV/';
- $z = strpos(strtoupper($causale), 'INV/') + 4;
- }
- if ($trovato) {
- $inizio = false;
- $fine = false;
- while ($z <= strlen($causale) && !$fine) {
- 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] === '-')) {
- $inizio = true;
- }
- if ($inizio) {
- 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] === '-') {
- $documento .= $causale[$z];
- } else {
- try {
- $annoDocumento = (int)trim(togliSpeciali(trim($documento)));
- if ($annoDocumento >= 2018 && $annoDocumento <= date('Y')) {
- $documento = '';
- $inizio = false;
- } else {
- $fine = true;
- }
- } catch (Exception $e) {
- $fine = true;
- }
- }
- }
- $z += 1;
- }
- }
- $y = strlen($causale);
- // cerco Data a 10 Caratteri
- while ($y >= 10 && !$trovato) {
- $dataDocumento = substr($causale, $y - 9, 10);
- if (in_array($dataDocumento[2], array('/', '.', '-', ' ')) && $dataDocumento[5] === $dataDocumento[2]) {
- try {
- $giorno = (int)substr($dataDocumento, 0, 2);
- $mese = (int)substr($dataDocumento, 3, 2);
- $anno = (int)substr($dataDocumento, 6, 4);
- if (checkdate($mese, $giorno, $anno) && date('Y') >= $anno) {
- $trovato = true;
- }
- } catch (Exception $e) {
- // dataDocumento := '';
- }
- }
- if (!$trovato && in_array($dataDocumento[4], array('/', '.', '-', ' ')) && $dataDocumento[7] === $dataDocumento[4]) {
- try {
- $anno = (int)substr($dataDocumento, 0, 4);
- $mese = (int)substr($dataDocumento, 5, 2);
- $giorno = (int)substr($dataDocumento, 8, 2);
- if (checkdate($mese, $giorno, $anno) && date('Y') >= $anno) {
- $trovato = true;
- }
- } catch (Exception $e) {
- // dataDocumento := '';
- }
- }
- $documento = '';
- $z = $y - 10;
- if ($trovato) {
- $tipoRegola = 'Data a 10 Caratteri';
- $inizio = false;
- $fine = false;
- while ($z > 1 && !$fine) {
- if (!$inizio && in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
- $inizio = true;
- }
- if ($inizio) {
- if ($inizio) {
- if (in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
- $documento = $causale[$z] . $documento;
- } else {
- try {
- $annoDocumento = (int)togliSpeciali(trim(togliSpeciali($documento)));
- if ($annoDocumento >= 2018 && $annoDocumento <= date('Y')) {
- $documento = '';
- $inizio = false;
- } else {
- $fine = true;
- }
- } catch (Exception $e) {
- $fine = true;
- }
- }
- }
- }
- $z -= 1;
- }
- } else {
- $dataDocumento = '';
- }
- $y -= 1;
- }
- // Data a 8 caratteri
- if (!$trovato) {
- $y = strlen($causale);
- while ($y >= 8 && !$trovato) {
- $dataDocumento = substr($causale, $y - 7, 8);
- if (in_array($dataDocumento[2], array('/', '.', '-', ' ')) && $dataDocumento[5] === $dataDocumento[2]) {
- try {
- $giorno = (int)substr($dataDocumento, 0, 2);
- $mese = (int)substr($dataDocumento, 3, 2);
- $anno = 2000 + (int)substr($dataDocumento, 6, 2);
- if (checkdate($mese, $giorno, $anno) && date('Y') >= $anno) {
- $trovato = true;
- }
- } catch (Exception $e) {
- }
- }
- if (!$trovato && in_array($dataDocumento[2], array('/', '.', '-', ' ')) && $dataDocumento[5] === $dataDocumento[2]) {
- try {
- $anno = 2000 + (int)substr($dataDocumento, 0, 2);
- $mese = (int)substr($dataDocumento, 3, 2);
- $giorno = (int)substr($dataDocumento, 6, 2);
- if (checkdate($mese, $giorno, $anno) && date('Y') >= $anno) {
- $trovato = true;
- }
- } catch (Exception $e) {
- }
- }
- $documento = '';
- $z = $y - 8;
- if ($trovato) {
- $tipoRegola = 'Data a 8 Caratteri';
- $inizio = false;
- $fine = false;
- while ($z > 1 && !$fine) {
- if (!$inizio && in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
- $inizio = true;
- }
- if ($inizio) {
- if ($inizio) {
- if (in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
- $documento = $causale[$z] . $documento;
- } else {
- try {
- $annoDocumento = (int)togliSpeciali(trim($documento));
- if ($annoDocumento >= 2018 && $annoDocumento <= date('Y')) {
- $documento = '';
- $inizio = false;
- } else {
- $fine = true;
- }
- } catch (Exception $e) {
- $fine = true;
- }
- }
- }
- }
- $z -= 1;
- }
- } else {
- $dataDocumento = '';
- }
- $y -= 1;
- }
- // Prendo gli ultimi caratteri per il numero
- if (!$trovato) {
- $tipoRegola = 'Documento';
- $z = strlen($causale);
- $inizio = false;
- $fine = false;
- while ($z >= 1 && !$fine) {
- 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] === '-')) {
- $inizio = true;
- }
- if ($inizio) {
- if ($inizio) {
- if (in_array($causale[$z], array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/', '-'))) {
- $documento = $causale[$z] . $documento;
- } else {
- try {
- $annoDocumento = (int)togliSpeciali(trim($documento));
- if ($annoDocumento >= 2018 && $annoDocumento <= date('Y')) {
- $documento = '';
- $inizio = false;
- } else {
- $fine = true;
- }
- } catch (Exception $e) {
- $fine = true;
- }
- }
- }
- }
- $z -= 1;
- }
- }
- }
- }
- }
- if (!empty($documento)) {
- //pulisco inizio e fine
- $primoCarattere = $documento[0];
- $ultimoCarattere = $documento[strlen($documento) - 1];
- if ($primoCarattere === '-' || $primoCarattere === '/') {
- $documento = substr($documento, 1);
- }
- if ($ultimoCarattere === '-' || $ultimoCarattere === '/') {
- $documento = substr($documento, 0, -1);
- }
- }
- $result = [];
- $result['causale'] = $causale;
- $result['numero_documento'] = $documento;
- $result['data_documento'] = $dataDocumento;
- $result['regola'] = $tipoRegola;
- return $result;
- }
- $CausaleABI = '48';
- $ABIBanca = '';
- $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.";
- $result = extractNumeroGold($CausaleABI,$descrHB,$ABIBanca);
- echo 'causale: ' .$result['causale'];
- echo "\n";
- echo 'regola: ' .$result['regola'];
- echo "\n";
- echo 'data_documento: ' .$result['data_documento'];
- echo "\n";
- echo 'numero_documento: ' .$result['numero_documento'];