Break down .txt file information for database save

0

function filedata () {     $ file = fopen ('data_centros / datos_AVC05.log', 'r');     if (! $ file) {         echo "The file could not be opened";     } else {         while (($ line = fgets ($ file))! == false) {             yield $ line;         }     } }

$ matches = array (); $ data = array ();

foreach (fileData () as $ line) {

if(preg_match('#^(?P<tipo>\w+)\/(?P<categoria>\w+)\s+(?P<rest>.+)#', $line, $coincidencias) === 1){

    $parts = explode(' ',preg_replace('/\s+/', ' ', $coincidencias['rest']));

    $data[] = [
        'id' => $parts[2],
        'fecha' => $parts[3],
        'hora' => $parts[4],
        'categoria' => $coincidencias['categoria'],
        'tipo' => $coincidencias['tipo']
    ];

        $datoss = "insert into ".$parts[2]." ".$parts[3]." ".$parts[4]." ".$coincidencias['categoria']." ".$coincidencias['tipo']." ";

} else {

    $data[count($data)-1]['descripcion'] .= $line;
    $line1 .=$line;
}

} echo $ line1; // print_r ($ data); echo $ datoss;

Data output

-->DIGITAL PATH QUALITY SUPERVISION
-->
-->
-->
-->ES
-->
-->DIP      DIPPART  ESL2   QSV    SECTION  DATE    TIME
-->
-->36GAMGS           26     54     0        181025  101510

insert into 628 181025 1101 APT O1 Array
(
    [id] => 628
    [fecha] => 181025
    [hora] => 1101
    [categoria] => APT
    [tipo] => O1
)

I want to do this basically:

insert into 628 181025 1101 APT O1 Array
(
    [id] => 628
    [fecha] => 181025
    [hora] => 1101
    [categoria] => APT
    [tipo] =>"PATH QUALITY SUPERVISION -ES DIP      DIPPART  ESL2   QSV    SECTION  DATE    TIME - 36GAMGS           26     54     0        181025  101510"

)
    
asked by Shack 22.10.2018 в 21:14
source

2 answers

0

Try to try the following so you can get an array where all the data you want to enter your table is in, and then what you would like to do is go through the data arrangement and call your insert

<?php 
$fileData = function(){
    $file = fopen('formato.txt', 'r');
    if(!$file){
        echo "No se pudo abrir el archivo";
    } else {
        while(($line = fgets($file)) !== false){
            yield $line;
        }
    }
};
$coincidencias = [];
$data = [];
foreach($fileData() as $line){
    if(preg_match('#^(?P<tipo>\w+)\/(?P<categoria>\w+)\s+(?P<rest>.+)#', $line, $coincidencias) === 1){
        $parts = explode(' ',preg_replace('/\s+/', ' ', $coincidencias['rest']));
        $data[] = [
            'id' => $parts[2],
            'fecha' => $parts[3],
            'hora' => $parts[4],
            'categoria' => $coincidencias['categoria'],
            'tipo' => $coincidencias['tipo'],
            'descripcion' => ''
        ];
    } else {
        $data[count($data)-1]['descripcion'] .= $line;
    }
}
print_r($data);
    
answered by 22.10.2018 в 21:58
0
function filedata(){
    $file = fopen('data_centrales/datos_AVC05.log', 'r');
    if(!$file){
        echo "No se pudo abrir el archivo";
    } else {
        while(($line = fgets($file)) !== false){
            yield $line;
        }
    }
}


$coincidencias = array();
$data = array();

foreach(fileData() as $line){

    if(preg_match('#^(?P<tipo>\w+)\/(?P<categoria>\w+)\s+(?P<rest>.+)#', $line, $coincidencias) === 1){

        $parts = explode(' ',preg_replace('/\s+/', ' ', $coincidencias['rest']));

        $data[] = [
            'id' => $parts[2],
            'fecha' => $parts[3],
            'hora' => $parts[4],
            'categoria' => $coincidencias['categoria'],
            'tipo' => $coincidencias['tipo']
        ];

            $datoss = "insert into ".$parts[2]." ".$parts[3]." ".$parts[4]." ".$coincidencias['categoria']." ".$coincidencias['tipo']." ";

    } else {

        $data[count($data)-1]['descripcion'] .= $line;
        $line1 .=$line;
    }

}
echo $line1;
//print_r($data);
echo $datoss;

I'm using it this way, but what happens is that the description must concatenate I have to make it in a whole line, and I do not know how to call it later, if it can be concatenated next to the Insert and More, I have to put a conditional the varible $line1 so that when it is another data, send the query and then continue with the following data

    
answered by 29.10.2018 в 22:03