Arbusto Arbusto - 17 days ago 5
PHP Question

Compare first array value with string inside if

The

if
statements in the following code are not working and I don't know why. When I use
var_dump
or
print_r
it shows the value correctly but the statements are not evaluating
True
.

<?php
$array_log = explode("\n", file_get_contents('log.txt'));
$ante_ultimo_elemento = count($array_log)-2;
$valor_ante_ultimo = $array_log[$ante_ultimo_elemento];


$valor_limpio = utf8_decode($valor_ante_ultimo);
$porciones1 = explode("=", $valor_limpio);

$valor_limpio2 = $porciones1[1];
$porciones2 = explode('?', $valor_limpio2);
$estado = $porciones2[0];

if($porciones2[0] === "Backup completato con successo"){
$estado_final = "COMPLETO";
}

if($porciones2[0] == "Nessun backup effettuato"){
$estado_final = 'CERO';
}

if($porciones2[0] == "Errore nella procedura di backup"){
$estado_final = 'ERROR';
}
?>


log.txt:

[Log Info]
BkpSet00.bks\Html-Log00.zip=Backup completato con successo÷00 : 00 : 59÷231466496
BkpSet00.bks\Html-Log01.zip=Errore nella procedura di backup÷00 : 00 : 02÷0
BkpSet00.bks\Html-Log02.zip=Backup completato con successo÷00 : 00 : 43÷216655360
BkpSet01.bks\Html-Log00.zip=Nessun backup effettuato÷00 : 00 : 02÷0
BkpSet01.bks\Html-Log01.zip=Errore nella procedura di backup÷00 : 00 : 46÷0
BkpSet02.bks\Html-Log00.zip=Nessun backup effettuato÷00 : 00 : 02÷0
BkpSet02.bks\Html-Log01.zip=Backup completato con successo÷00 : 00 : 14÷216655360
BkpSet03.bks\Html-Log00.zip=Nessun backup effettuato÷00 : 00 : 02÷0
BkpSet03.bks\Html-Log01.zip=Backup completato con successo÷00 : 00 : 17÷231663104
BkpSet04.bks\Html-Log00.zip=Backup completato con successo÷00 : 00 : 16÷231663104
BkpSet00.bks\Html-Log03.zip=Backup completato con successo÷00 : 00 : 13÷216655360
BkpSet00.bks\Html-Log04.zip=Backup completato con successo÷00 : 00 : 09÷216655360
BkpSet16.bks\Html-Log00.zip=Backup completato con successo÷00 : 01 : 33÷1462158767
BkpSet05.bks\Html-Log00.zip=Errore nella procedura di backup÷11 : 18 : 30÷235564152142


Thank you!

Answer

This piece of code does the trick. Note that since 3v4l site can't read from a file, I have to make a substitution. just omit that part

// read from file. this function puts file content in an array line by line
$array_log = file('log.txt');
// finding the desired element. count()-2 means the one before the final entry. count()-1 means the final entry.
$ante_ultimo_elemento =  count($array_log)-2;
// taking the entry apart into its pieces using regex.
preg_match('/(?P<file>.*?)=(?P<message>[\w\s]*?)÷(?P<time>\d{2}\s?:\s?\d{2}\s?:\s?\d{2}\s?)÷(?<last_part>\d*)/', $array_log[$ante_ultimo_elemento], $estado);
// to avoid notice on the occasion that no statement could be matched, I highly recommend using if-elseif-else statement to avoid unwanted situations;
// or better still, use switch-case-default
switch($estado['message'])
{
    case "Backup completato con successo": {
        $estado_final = "COMPLETO";
        break;
    }
    case "Nessun backup effettuato": {
        $estado_final = 'CERO';
        break;
    }
    case "Errore nella procedura di backup": {
        $estado_final = 'ERROR';
        break;
    }
    default: {
        $estado_final = 'FATAL ERROR';
        break;
    }
}
echo $estado_final;