Read TXT file with PHP and write to mysql

1

I am developing an application to control the equipment of the company, I can generate a log of the equipment with all the information related to the equipment, however this file is not tabulated, example below a few lines:

SET SYS:SYSDESC="SQL01ALD", SYSOBJECTID="SQL01ALD", SYSSERVICES="UMTS";
ADD URNCBASIC:SQLID=490, NSAP="H'450551310008501F000000000000000000000000";
ADD UCNOPERATOR:CNOPINDEX=0, MCC="724", MNC="31", OPERATORTYPE=PRIM;
ADD OPC:SPX=0, SPDF=WNF, SPC=16360, NAME="SQL01ALD";
ADD N7DPC:DPX=1, SPDF=WNF, NAME="MSS68ALD", DPCT=IUCS_RANAP;
ADD N7DPC:DPX=2, SPDF=WNF, NAME="MGW04ALD", DPCT=IUCS_ALCAP;
ADD N7DPC:DPX=3, SPDF=WNF, NAME="SQL01ALD";
ADD N7DPC:DPX=4, SPDF=WNF, NAME="SQL01FLZ";
ADD N7DPC:DPX=5, SPDF=WNF, NAME="SQL02ALD";

The basic intention is to take a field as a reference, for example the field SYSDESC="SQL01ALD" copy only the SQL01ALD to the database.

For the logic of writing to the database, I thought of looping with while by taking the file and writing to mysql .

The X of the question is how to get the field I want.

    
asked by anonymous 04.09.2015 / 20:04

1 answer

1
$file = 'file.txt';

$resource = fopen($file, 'r');

$lines = '';
while(!feof($resource)){
    $lines[] = fgets($resource, 4096);
}

$fullVar = true;
$fullVar = $fullVar ? ':' : ''; 

$er = '~([\w%s]+)=("[^"]+"|\w+)~';

$varsFile = array();
foreach ($lines as $k => $value) {
    preg_match_all(sprintf($er, $fullVar), $value, $match);
    foreach ($match[1] as $kk => $result) {
        $varsFile[$k][$match[1][$kk]] = trim($match[2][$kk], '"');
    }
}

// AQUI VOCE TERA AS VARIAZEIS DE CADA LINHA
foreach ($varsFile as $linha => $vars){
    # code...
}

If you want to include the entire variable name, just keep $fullVar as true , otherwise change to false as it will capture the reduced variable.

    
05.09.2015 / 18:10