Help with PHP in data capture.

0

Hello, I have a particular log file, and I would like to capture certain data from it and store it in [].

view the log file:

+++ Statistics Dump +++ (1499195839)
++ Incoming Requests ++
                 552 QUERY
++ Incoming Queries ++
                 546 A
                   1 NS
                   5 AAAA
++ Outgoing Queries ++
[View: default]
                1350 A
                  53 NS
                 446 AAAA
                 544 DS
                  35 DNSKEY
[View: _bind]
++ Name Server Statistics ++
                 552 IPv4 requests received
                   1 requests with EDNS(0) received
                 550 responses sent
                   1 responses with EDNS(0) sent
                 300 queries resulted in successful answer
                 541 queries resulted in non authoritative answer
                   6 queries resulted in nxrrset
                   9 queries resulted in SERVFAIL
                 235 queries resulted in NXDOMAIN
                 385 queries caused recursion
                   2 duplicate queries received
++ Zone Maintenance Statistics ++
++ Resolver Statistics ++
[Common]

Now via php I would like to take for example the 3 line the given 552 and print like this: [552].

Thank you.

    
asked by anonymous 04.07.2017 / 21:27

2 answers

0

You can use regex with preg_match_all and read row by line, regex should look like this (more or less, you can adjust):

\s+(\d{1,3})\s[^\n]+\n

^   ^       ^  ^   ^
|   |       |  |   |
|   |       |  |   |
|   |       |  |   +--- Procura a quebra de linha (para separar os resultados)
|   |       |  |
|   |       |  +---Procura qualquer coisa que não seja quebra de linha
|   |       |  
|   |       +-- Verifica se há um espaço após o numero
|   |       
|   +-- Pega o numero (o parenteses é para separar e um novo 'match')
|   
+------ procura qualquer espaço até achar o numero

Code should look like this:

<?php
$foo = file_get_contents('meulog.log');

if (preg_match_all('#\s+(\d{1,3})\s[^\n]+\n#', $foo, $matches)) {
    $retornou = $matches[1];
    print_r($retornou); //Exibe todos numeros que pegou
} else {
    echo 'Nada encontrado';
}

Example online at IDEONE , code:

<?php
$foo = '+++ Statistics Dump +++ (1499195839)
++ Incoming Requests ++
                 552 QUERY
++ Incoming Queries ++
                 546 A
                   1 NS
                   5 AAAA
++ Outgoing Queries ++
[View: default]
                1350 A
                  53 NS
                 446 AAAA
                 544 DS
                  35 DNSKEY
[View: _bind]
++ Name Server Statistics ++
                 552 IPv4 requests received
                   1 requests with EDNS(0) received
                 550 responses sent
                   1 responses with EDNS(0) sent
                 300 queries resulted in successful answer
                 541 queries resulted in non authoritative answer
                   6 queries resulted in nxrrset
                   9 queries resulted in SERVFAIL
                 235 queries resulted in NXDOMAIN
                 385 queries caused recursion
                   2 duplicate queries received
++ Zone Maintenance Statistics ++
++ Resolver Statistics ++
[Common]';

if (preg_match_all('#\s+(\d{1,3})\s[^\n]+\n#', $foo, $matches)) {
    $retornou = $matches[1];
    print_r($retornou); //Exibe todos numeros que pegou
} else {
    echo 'Nada encontrado';
}

The example returns this:

Array
(
    [0] => 552
    [1] => 546
    [2] => 1
    [3] => 5
    [4] => 53
    [5] => 446
    [6] => 544
    [7] => 35
    [8] => 552
    [9] => 1
    [10] => 550
    [11] => 1
    [12] => 300
    [13] => 541
    [14] => 6
    [15] => 9
    [16] => 235
    [17] => 385
    [18] => 2
)

Just iterate with a for or while

  

Perfect, but makes me leave the result inside the [] by   example:

     

0 = > [552] 1 = > [546]

You can use array_map or a for normal, eg:

if (preg_match_all('#\s+(\d{1,3})\s[^\n]+\n#', $foo, $matches)) {
    $retornou = array_map(function ($value) {
        //Adiciona os colchetes (parênteses retos)
        return '[' . $value . ']';
    }, $matches[1]);

    print_r($retornou); //Exibe todos numeros que pegou
} else {
    echo 'Nada encontrado';
}
    
04.07.2017 / 22:56
-1

Perfect, but makes me leave the result inside the [] for example:

0=> [552] 1=> [546]

    
05.07.2017 / 12:58